Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,55 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
from flask import Flask, render_template, request, jsonify
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
-
current_text = ""
|
7 |
-
logs = []
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
@app.route('/')
|
18 |
-
def
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
return jsonify({'text': current_text})
|
40 |
-
|
41 |
-
@app.route('/log')
|
42 |
-
def log():
|
43 |
-
return render_template('log.html', log_data=logs)
|
44 |
|
45 |
if __name__ == '__main__':
|
46 |
-
app.run(debug=True, host=
|
|
|
1 |
+
from flask import Flask, request, jsonify, render_template_string
|
2 |
+
from datetime import datetime
|
|
|
3 |
|
4 |
app = Flask(__name__)
|
|
|
|
|
5 |
|
6 |
+
# テキストを記録するためのリスト
|
7 |
+
texts = []
|
8 |
+
|
9 |
+
@app.route('/post', methods=['POST'])
|
10 |
+
def post_text():
|
11 |
+
# POSTリクエストからテキストを取得
|
12 |
+
text = request.form.get('text')
|
13 |
+
if text:
|
14 |
+
# 現在の日時を取得
|
15 |
+
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
16 |
+
# テキストとタイムスタンプをリストに追加
|
17 |
+
texts.append({'text': text, 'timestamp': timestamp})
|
18 |
+
return jsonify({'message': 'Text received and recorded', 'text': text, 'timestamp': timestamp}), 201
|
19 |
+
else:
|
20 |
+
return jsonify({'message': 'No text provided'}), 400
|
21 |
+
|
22 |
+
@app.route('/getText/latest', methods=['GET'])
|
23 |
+
def get_latest_text():
|
24 |
+
if texts:
|
25 |
+
# リストの最後の要素(最新のテキスト)を取得
|
26 |
+
latest_text = texts[-1]
|
27 |
+
return jsonify(latest_text), 200
|
28 |
+
else:
|
29 |
+
return jsonify({'message': 'No texts recorded yet'}), 200
|
30 |
|
31 |
@app.route('/')
|
32 |
+
def display_texts():
|
33 |
+
# 記録されたテキストをHTMLで表示
|
34 |
+
html_content = '''
|
35 |
+
<!DOCTYPE html>
|
36 |
+
<html lang="ja">
|
37 |
+
<head>
|
38 |
+
<meta charset="UTF-8">
|
39 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
40 |
+
<title>Recorded Texts</title>
|
41 |
+
</head>
|
42 |
+
<body>
|
43 |
+
<h1>Recorded Texts</h1>
|
44 |
+
<ul>
|
45 |
+
{% for entry in texts %}
|
46 |
+
<li><strong>{{ entry.timestamp }}:</strong> {{ entry.text }}</li>
|
47 |
+
{% endfor %}
|
48 |
+
</ul>
|
49 |
+
</body>
|
50 |
+
</html>
|
51 |
+
'''
|
52 |
+
return render_template_string(html_content, texts=texts)
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
if __name__ == '__main__':
|
55 |
+
app.run(debug=True, host='0.0.0.0', port=7860)
|