-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathserver.py.prompt.txt
More file actions
450 lines (369 loc) · 15.7 KB
/
server.py.prompt.txt
File metadata and controls
450 lines (369 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
claude-3-5-sonnet-20240620
You are Claude, an AI assistant powered by Anthropic's Claude-3.5-Sonnet model, specialized in backend development.
You are good at writing python webserver in single self-contained python files.
1. Regarding data or database.
If provide API key and mention API to use, generate client to actually connect to the API. (assume API is accesible and key is correct.)
Otherwise, generate mock data and prefer to use in-memory data-structure database.
When use sqlite, use local file database (in current directory).
2. You may use libraries like Flask, websocket and etc.
When use Flask, also enable CORS.
3. Bind to `localhost:9079`.
Output python source code should be included in <SERVER_PYTHON_CODE></SERVER_PYTHON_CODE> block.
=== 0: user ===
Generate "server.py". Backend design:
```
The backend will be a Python Flask server with the following components:
1. Main Flask app:
- Import necessary libraries (Flask, difflib)
- Create Flask app instance
2. Route: "/"
- Serve the main HTML page
3. Route: "/compare" (POST method)
- Receive two texts from the frontend
- Use difflib to compare the texts and generate HTML diff
- Return the HTML diff to the frontend
4. Helper function: generate_diff(text1, text2)
- Use difflib.HtmlDiff().make_file() to create an HTML diff
- Customize the output to make it more visually appealing
5. Run the Flask app
The server will use the difflib library to generate a colorized HTML diff between the two input texts. This diff will highlight additions, deletions, and changes between the two versions.
```
=== 1: assistant ===
Certainly! I'll create a "server.py" file based on your backend design specifications. Here's the Python code for the Flask server:
<SERVER_PYTHON_CODE>
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import difflib
import os
app = Flask(__name__)
CORS(app)
@app.route('/')
def serve_html():
return send_from_directory('.', 'index.html')
@app.route('/compare', methods=['POST'])
def compare_texts():
data = request.json
text1 = data.get('text1', '')
text2 = data.get('text2', '')
diff_html = generate_diff(text1, text2)
return jsonify({'diff': diff_html})
def generate_diff(text1, text2):
differ = difflib.HtmlDiff(wrapcolumn=70)
diff = differ.make_file(text1.splitlines(), text2.splitlines())
# Customize the diff output
custom_diff = diff.replace(
'<table class="diff" id="difflib_chg_to0__top"',
'<table class="diff" id="difflib_chg_to0__top" style="width:100%; border-collapse: collapse;"'
)
custom_diff = custom_diff.replace(
'<td class="diff_header"',
'<td class="diff_header" style="padding: 5px; text-align: right; width: 40px; background-color: #f0f0f0;"'
)
custom_diff = custom_diff.replace(
'<td nowrap="nowrap"',
'<td nowrap="nowrap" style="padding: 5px; white-space: pre-wrap; font-family: monospace;"'
)
return custom_diff
if __name__ == '__main__':
app.run(host='localhost', port=9079, debug=True)
</SERVER_PYTHON_CODE>
This "server.py" file implements the backend design you specified. Here's a breakdown of the components:
1. Main Flask app:
- We import the necessary libraries: Flask, jsonify, request, send_from_directory, CORS, and difflib.
- We create a Flask app instance and enable CORS.
2. Route: "/"
- The root route serves the main HTML page (assuming there's an index.html file in the same directory).
3. Route: "/compare" (POST method)
- This route receives two texts from the frontend via a POST request.
- It calls the generate_diff function to create an HTML diff.
- The HTML diff is returned to the frontend as JSON.
4. Helper function: generate_diff(text1, text2)
- This function uses difflib.HtmlDiff().make_file() to create an HTML diff.
- The output is customized to make it more visually appealing by adding inline styles.
5. Run the Flask app
- The app is set to run on localhost:9079 in debug mode.
To use this server:
1. Save this code as "server.py" in your project directory.
2. Make sure you have Flask and flask-cors installed (`pip install flask flask-cors`).
3. Run the server using `python server.py`.
The server will start on http://localhost:9079. You can then send POST requests to the "/compare" endpoint with JSON data containing "text1" and "text2" to get the HTML diff.
Note that this server expects an "index.html" file in the same directory to serve the main page. You'll need to create that file separately or modify the root route to serve your frontend content as needed.
=== 2: user ===
Generate "index.html" that connect to the server. The "index.html" design:
```
The frontend will be a single HTML page with embedded CSS and JavaScript:
1. HTML structure:
- Title: "Text Comparison Tool"
- Two large text areas for input:
- Left: "Original Text"
- Right: "Modified Text"
- "Compare" button
- Result area to display the diff
2. CSS styling:
- Modern, clean design with a color scheme (e.g., blue and white)
- Responsive layout for various screen sizes
- Styled text areas and button
- Custom styling for diff output (highlighted additions and deletions)
3. JavaScript functionality:
- Event listener for the "Compare" button
- AJAX call to send texts to the backend "/compare" route
- Function to display the returned HTML diff in the result area
- Error handling for failed requests or empty inputs
4. Additional features:
- "Clear" button to reset both text areas
- "Swap" button to switch the contents of the two text areas
- Loading spinner while waiting for the comparison result
The frontend will provide an intuitive interface for users to input their texts, initiate the comparison, and view the results in a visually appealing manner.
```
=== 3: assistant ===
Certainly! I'll create an "index.html" file that connects to the server based on your design specifications. Here's the HTML code with embedded CSS and JavaScript:
<INDEX_HTML_CODE>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Comparison Tool</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #f4f4f4;
}
h1 {
text-align: center;
color: #2c3e50;
}
.container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.text-area {
flex: 1 1 45%;
min-width: 300px;
}
textarea {
width: 100%;
height: 300px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
resize: vertical;
}
.buttons {
display: flex;
justify-content: center;
gap: 10px;
margin: 20px 0;
}
button {
padding: 10px 20px;
background-color: #3498db;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #2980b9;
}
#result {
background-color: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.spinner {
display: none;
width: 50px;
height: 50px;
border: 3px solid #f3f3f3;
border-top: 3px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
margin: 20px auto;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@media (max-width: 768px) {
.text-area {
flex: 1 1 100%;
}
}
</style>
</head>
<body>
<h1>Text Comparison Tool</h1>
<div class="container">
<div class="text-area">
<h2>Original Text</h2>
<textarea id="text1" placeholder="Enter original text here..."></textarea>
</div>
<div class="text-area">
<h2>Modified Text</h2>
<textarea id="text2" placeholder="Enter modified text here..."></textarea>
</div>
</div>
<div class="buttons">
<button id="compareBtn">Compare</button>
<button id="clearBtn">Clear</button>
<button id="swapBtn">Swap</button>
</div>
<div class="spinner" id="spinner"></div>
<div id="result"></div>
<script>
const text1 = document.getElementById('text1');
const text2 = document.getElementById('text2');
const compareBtn = document.getElementById('compareBtn');
const clearBtn = document.getElementById('clearBtn');
const swapBtn = document.getElementById('swapBtn');
const result = document.getElementById('result');
const spinner = document.getElementById('spinner');
compareBtn.addEventListener('click', compareTexts);
clearBtn.addEventListener('click', clearTexts);
swapBtn.addEventListener('click', swapTexts);
function compareTexts() {
const originalText = text1.value.trim();
const modifiedText = text2.value.trim();
if (!originalText || !modifiedText) {
alert('Please enter both original and modified texts.');
return;
}
spinner.style.display = 'block';
result.innerHTML = '';
fetch('http://localhost:9079/compare', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text1: originalText, text2: modifiedText }),
})
.then(response => response.json())
.then(data => {
spinner.style.display = 'none';
result.innerHTML = data.diff;
})
.catch(error => {
spinner.style.display = 'none';
console.error('Error:', error);
result.innerHTML = 'An error occurred while comparing the texts. Please try again.';
});
}
function clearTexts() {
text1.value = '';
text2.value = '';
result.innerHTML = '';
}
function swapTexts() {
const temp = text1.value;
text1.value = text2.value;
text2.value = temp;
}
</script>
</body>
</html>
</INDEX_HTML_CODE>
This "index.html" file implements the frontend design you specified. Here's a breakdown of the components:
1. HTML structure:
- Title: "Text Comparison Tool"
- Two large text areas for input: "Original Text" and "Modified Text"
- "Compare", "Clear", and "Swap" buttons
- Result area to display the diff
- Loading spinner
2. CSS styling:
- Modern, clean design with a blue and white color scheme
- Responsive layout using flexbox
- Styled text areas and buttons
- Custom styling for the result area
- Loading spinner animation
3. JavaScript functionality:
- Event listeners for the "Compare", "Clear", and "Swap" buttons
- AJAX call using fetch() to send texts to the backend "/compare" route
- Function to display the returned HTML diff in the result area
- Error handling for failed requests or empty inputs
4. Additional features:
- "Clear" button to reset both text areas
- "Swap" button to switch the contents of the two text areas
- Loading spinner while waiting for the comparison result
This frontend provides an intuitive interface for users to input their texts, initiate the comparison, and view the results in a visually appealing manner. It connects to the server running at http://localhost:9079 and sends POST requests to the "/compare" endpoint.
To use this frontend:
1. Save this code as "index.html" in the same directory as your "server.py" file.
2. Make sure your Flask server is running (python server.py).
3. Open the "index.html" file in a web browser.
You should now have a fully functional Text Comparison Tool that connects to your Flask backend and provides a user-friendly interface for comparing texts.
=== 4: user ===
Modify "server.py" to also serve "index.html" like a static web server.
=== Reply 0 ===
Certainly! I'll modify the "server.py" file to serve the "index.html" file as a static web server. This way, you won't need a separate web server to host your frontend. Here's the updated "server.py" code:
<SERVER_PYTHON_CODE>
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import difflib
import os
app = Flask(__name__, static_folder='.')
CORS(app)
@app.route('/')
def serve_html():
return send_from_directory('.', 'index.html')
@app.route('/<path:path>')
def serve_static(path):
return send_from_directory('.', path)
@app.route('/compare', methods=['POST'])
def compare_texts():
data = request.json
text1 = data.get('text1', '')
text2 = data.get('text2', '')
diff_html = generate_diff(text1, text2)
return jsonify({'diff': diff_html})
def generate_diff(text1, text2):
differ = difflib.HtmlDiff(wrapcolumn=70)
diff = differ.make_file(text1.splitlines(), text2.splitlines())
# Customize the diff output
custom_diff = diff.replace(
'<table class="diff" id="difflib_chg_to0__top"',
'<table class="diff" id="difflib_chg_to0__top" style="width:100%; border-collapse: collapse;"'
)
custom_diff = custom_diff.replace(
'<td class="diff_header"',
'<td class="diff_header" style="padding: 5px; text-align: right; width: 40px; background-color: #f0f0f0;"'
)
custom_diff = custom_diff.replace(
'<td nowrap="nowrap"',
'<td nowrap="nowrap" style="padding: 5px; white-space: pre-wrap; font-family: monospace;"'
)
return custom_diff
if __name__ == '__main__':
app.run(host='localhost', port=9079, debug=True)
</SERVER_PYTHON_CODE>
Here are the changes made to the "server.py" file:
1. Modified the Flask app initialization to set the current directory as the static folder:
```python
app = Flask(__name__, static_folder='.')
```
2. Updated the root route to serve the "index.html" file:
```python
@app.route('/')
def serve_html():
return send_from_directory('.', 'index.html')
```
3. Added a new route to serve any static files (including the "index.html" file):
```python
@app.route('/<path:path>')
def serve_static(path):
return send_from_directory('.', path)
```
These changes allow the Flask server to serve the "index.html" file and any other static files (like CSS or JavaScript files if you decide to separate them in the future) directly from the same directory as the "server.py" file.
To use this updated server:
1. Save the modified "server.py" in the same directory as your "index.html" file.
2. Run the server using `python server.py`.
3. Open a web browser and navigate to `http://localhost:9079`.
The Flask server will now serve your "index.html" file, and you can use the Text Comparison Tool directly from this URL without needing a separate web server for the frontend.
This setup is more convenient for development and testing, as you only need to run a single server to handle both the frontend and backend. However, for production deployments, you might want to consider using a dedicated web server (like Nginx) to serve static files and proxy requests to your Flask application for better performance and security.