Spaces:
Sleeping
Sleeping
| from flask import Flask, request, jsonify, render_template_string | |
| from datetime import datetime | |
| app = Flask(__name__) | |
| # テキストを記録するためのリスト | |
| texts = [] | |
| def post_text(): | |
| # POSTリクエストからテキストを取得 | |
| text = request.form.get('text') | |
| if text: | |
| # 現在の日時を取得 | |
| timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
| # テキストとタイムスタンプをリストに追加 | |
| texts.append({'text': text, 'timestamp': timestamp}) | |
| return jsonify({'message': 'Text received and recorded', 'text': text, 'timestamp': timestamp}), 201 | |
| else: | |
| return jsonify({'message': 'No text provided'}), 400 | |
| def get_latest_text(): | |
| if texts: | |
| # リストの最後の要素(最新のテキスト)を取得 | |
| latest_text = texts[-1] | |
| return jsonify(latest_text), 200 | |
| else: | |
| return jsonify({'message': 'No texts recorded yet'}), 200 | |
| def display_texts(): | |
| # 記録されたテキストをHTMLで表示 | |
| html_content = ''' | |
| <!DOCTYPE html> | |
| <html lang="ja"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Recorded Texts</title> | |
| </head> | |
| <body> | |
| <h1>Recorded Texts</h1> | |
| <ul> | |
| {% for entry in texts %} | |
| <li><strong>{{ entry.timestamp }}:</strong> {{ entry.text }}</li> | |
| {% endfor %} | |
| </ul> | |
| </body> | |
| </html> | |
| ''' | |
| return render_template_string(html_content, texts=texts) | |
| if __name__ == '__main__': | |
| app.run(debug=True, host='0.0.0.0', port=7860) | |