AntDX316
commited on
Commit
·
c0d0d43
1
Parent(s):
ab59b5c
updated
Browse files- app.py +126 -0
- requirements.txt +2 -0
- templates/index.html +88 -0
app.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
import os
|
3 |
+
import datetime
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# Ensure templates directory exists
|
8 |
+
os.makedirs('templates', exist_ok=True)
|
9 |
+
|
10 |
+
@app.route('/')
|
11 |
+
def index():
|
12 |
+
"""Render the home page."""
|
13 |
+
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
14 |
+
return render_template('index.html', current_time=current_time)
|
15 |
+
|
16 |
+
@app.route('/api/time')
|
17 |
+
def get_time():
|
18 |
+
"""API endpoint that returns the current time."""
|
19 |
+
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
20 |
+
return jsonify({"time": current_time})
|
21 |
+
|
22 |
+
@app.route('/api/echo', methods=['POST'])
|
23 |
+
def echo():
|
24 |
+
"""API endpoint that echoes back the JSON data sent to it."""
|
25 |
+
data = request.json
|
26 |
+
return jsonify({"status": "success", "echo": data})
|
27 |
+
|
28 |
+
if __name__ == '__main__':
|
29 |
+
# Create templates directory if it doesn't exist
|
30 |
+
if not os.path.exists('templates'):
|
31 |
+
os.makedirs('templates')
|
32 |
+
|
33 |
+
# Create a simple HTML template if it doesn't exist
|
34 |
+
if not os.path.exists('templates/index.html'):
|
35 |
+
with open('templates/index.html', 'w') as f:
|
36 |
+
f.write('''
|
37 |
+
<!DOCTYPE html>
|
38 |
+
<html>
|
39 |
+
<head>
|
40 |
+
<title>Flask Web App</title>
|
41 |
+
<style>
|
42 |
+
body {
|
43 |
+
font-family: Arial, sans-serif;
|
44 |
+
max-width: 800px;
|
45 |
+
margin: 0 auto;
|
46 |
+
padding: 20px;
|
47 |
+
line-height: 1.6;
|
48 |
+
}
|
49 |
+
.container {
|
50 |
+
border: 1px solid #ddd;
|
51 |
+
border-radius: 5px;
|
52 |
+
padding: 20px;
|
53 |
+
margin-top: 20px;
|
54 |
+
}
|
55 |
+
button {
|
56 |
+
background-color: #4CAF50;
|
57 |
+
color: white;
|
58 |
+
padding: 10px 15px;
|
59 |
+
border: none;
|
60 |
+
border-radius: 4px;
|
61 |
+
cursor: pointer;
|
62 |
+
margin-right: 10px;
|
63 |
+
}
|
64 |
+
button:hover {
|
65 |
+
background-color: #45a049;
|
66 |
+
}
|
67 |
+
pre {
|
68 |
+
background-color: #f5f5f5;
|
69 |
+
padding: 10px;
|
70 |
+
border-radius: 4px;
|
71 |
+
overflow-x: auto;
|
72 |
+
}
|
73 |
+
</style>
|
74 |
+
</head>
|
75 |
+
<body>
|
76 |
+
<h1>Welcome to Flask Web App</h1>
|
77 |
+
<p>The current server time is: {{ current_time }}</p>
|
78 |
+
|
79 |
+
<div class="container">
|
80 |
+
<h2>API Demo</h2>
|
81 |
+
<button id="getTimeBtn">Get Current Time</button>
|
82 |
+
<button id="sendEchoBtn">Send Echo Request</button>
|
83 |
+
<div id="result">
|
84 |
+
<pre id="output">Results will appear here...</pre>
|
85 |
+
</div>
|
86 |
+
</div>
|
87 |
+
|
88 |
+
<script>
|
89 |
+
document.getElementById('getTimeBtn').addEventListener('click', async () => {
|
90 |
+
try {
|
91 |
+
const response = await fetch('/api/time');
|
92 |
+
const data = await response.json();
|
93 |
+
document.getElementById('output').textContent = JSON.stringify(data, null, 2);
|
94 |
+
} catch (error) {
|
95 |
+
document.getElementById('output').textContent = 'Error: ' + error.message;
|
96 |
+
}
|
97 |
+
});
|
98 |
+
|
99 |
+
document.getElementById('sendEchoBtn').addEventListener('click', async () => {
|
100 |
+
try {
|
101 |
+
const testData = {
|
102 |
+
message: "Hello from the client!",
|
103 |
+
timestamp: new Date().toISOString()
|
104 |
+
};
|
105 |
+
|
106 |
+
const response = await fetch('/api/echo', {
|
107 |
+
method: 'POST',
|
108 |
+
headers: {
|
109 |
+
'Content-Type': 'application/json'
|
110 |
+
},
|
111 |
+
body: JSON.stringify(testData)
|
112 |
+
});
|
113 |
+
|
114 |
+
const data = await response.json();
|
115 |
+
document.getElementById('output').textContent = JSON.stringify(data, null, 2);
|
116 |
+
} catch (error) {
|
117 |
+
document.getElementById('output').textContent = 'Error: ' + error.message;
|
118 |
+
}
|
119 |
+
});
|
120 |
+
</script>
|
121 |
+
</body>
|
122 |
+
</html>
|
123 |
+
''')
|
124 |
+
|
125 |
+
# Run the Flask app
|
126 |
+
app.run(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
flask==2.3.3
|
2 |
+
Werkzeug==2.3.7
|
templates/index.html
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
<!DOCTYPE html>
|
3 |
+
<html>
|
4 |
+
<head>
|
5 |
+
<title>Flask Web App</title>
|
6 |
+
<style>
|
7 |
+
body {
|
8 |
+
font-family: Arial, sans-serif;
|
9 |
+
max-width: 800px;
|
10 |
+
margin: 0 auto;
|
11 |
+
padding: 20px;
|
12 |
+
line-height: 1.6;
|
13 |
+
}
|
14 |
+
.container {
|
15 |
+
border: 1px solid #ddd;
|
16 |
+
border-radius: 5px;
|
17 |
+
padding: 20px;
|
18 |
+
margin-top: 20px;
|
19 |
+
}
|
20 |
+
button {
|
21 |
+
background-color: #4CAF50;
|
22 |
+
color: white;
|
23 |
+
padding: 10px 15px;
|
24 |
+
border: none;
|
25 |
+
border-radius: 4px;
|
26 |
+
cursor: pointer;
|
27 |
+
margin-right: 10px;
|
28 |
+
}
|
29 |
+
button:hover {
|
30 |
+
background-color: #45a049;
|
31 |
+
}
|
32 |
+
pre {
|
33 |
+
background-color: #f5f5f5;
|
34 |
+
padding: 10px;
|
35 |
+
border-radius: 4px;
|
36 |
+
overflow-x: auto;
|
37 |
+
}
|
38 |
+
</style>
|
39 |
+
</head>
|
40 |
+
<body>
|
41 |
+
<h1>Welcome to Flask Web App</h1>
|
42 |
+
<p>The current server time is: {{ current_time }}</p>
|
43 |
+
|
44 |
+
<div class="container">
|
45 |
+
<h2>API Demo</h2>
|
46 |
+
<button id="getTimeBtn">Get Current Time</button>
|
47 |
+
<button id="sendEchoBtn">Send Echo Request</button>
|
48 |
+
<div id="result">
|
49 |
+
<pre id="output">Results will appear here...</pre>
|
50 |
+
</div>
|
51 |
+
</div>
|
52 |
+
|
53 |
+
<script>
|
54 |
+
document.getElementById('getTimeBtn').addEventListener('click', async () => {
|
55 |
+
try {
|
56 |
+
const response = await fetch('/api/time');
|
57 |
+
const data = await response.json();
|
58 |
+
document.getElementById('output').textContent = JSON.stringify(data, null, 2);
|
59 |
+
} catch (error) {
|
60 |
+
document.getElementById('output').textContent = 'Error: ' + error.message;
|
61 |
+
}
|
62 |
+
});
|
63 |
+
|
64 |
+
document.getElementById('sendEchoBtn').addEventListener('click', async () => {
|
65 |
+
try {
|
66 |
+
const testData = {
|
67 |
+
message: "Hello from the client!",
|
68 |
+
timestamp: new Date().toISOString()
|
69 |
+
};
|
70 |
+
|
71 |
+
const response = await fetch('/api/echo', {
|
72 |
+
method: 'POST',
|
73 |
+
headers: {
|
74 |
+
'Content-Type': 'application/json'
|
75 |
+
},
|
76 |
+
body: JSON.stringify(testData)
|
77 |
+
});
|
78 |
+
|
79 |
+
const data = await response.json();
|
80 |
+
document.getElementById('output').textContent = JSON.stringify(data, null, 2);
|
81 |
+
} catch (error) {
|
82 |
+
document.getElementById('output').textContent = 'Error: ' + error.message;
|
83 |
+
}
|
84 |
+
});
|
85 |
+
</script>
|
86 |
+
</body>
|
87 |
+
</html>
|
88 |
+
|