-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_example_w_persistent_storage_app.py
122 lines (98 loc) · 3.39 KB
/
flask_example_w_persistent_storage_app.py
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
# flask example with persistent storage across sessions using
# a local .json file to record the latest get requests, values, and timestamp
#
# run with: streamlit run flask_example_w_persistent_storage_app.py
#
# send in get requests on 8502 with: curl "http://localhost:8502/?param1=test¶m2=example"
#
# browser in streamlit on 8501 displays parameters passed in via command line
# WORKS LOCALLY BUT FLASK IS NOT SUPPORTED ON STREAMLIT COMMUNITY CLOUD
# mdc, 15 Dec 2024
import threading
import streamlit as st
from flask import Flask, request, jsonify
import time
import json
from datetime import datetime
import os
# Flask app
flask_app = Flask(__name__)
# File to store GET request data
DATA_FILE = "received_requests.json"
# Load saved data if it exists
if os.path.exists(DATA_FILE):
with open(DATA_FILE, "r") as file:
saved_data = json.load(file)
else:
saved_data = []
# Shared data dictionary to store the latest GET request
shared_data = {}
@flask_app.route('/', methods=['GET'])
def handle_get_request():
global shared_data
# Get query parameters as a dictionary
shared_data = request.args.to_dict()
# Add a timestamp to the received data
timestamp = datetime.now().isoformat()
entry = {"timestamp": timestamp, "data": shared_data, 'buttonCleared': False}
# Save the new entry to the file
saved_data.append(entry)
with open(DATA_FILE, "w") as file:
json.dump(saved_data, file, indent=4)
return jsonify({"message": "GET request received", "data": shared_data}), 200
# Function to run Flask in a separate thread
def run_flask():
flask_app.run(host="0.0.0.0", port=8502, debug=False, use_reloader=False)
# Start Flask in a background thread
flask_thread = threading.Thread(target=run_flask, daemon=True)
flask_thread.start()
# Streamlit interface
st.title("Streamlit and Flask GET Request Example")
st.write("Use the following curl command to send a GET request:")
st.code('curl "http://localhost:8502/?param1=value1¶m2=value2"', language="bash")
st.markdown(
"""
<style>
.blue-box {
background-color: #e0f7ff;
padding: 10px;
border-radius: 5px;
margin-top: 20px;
}
</style>
""",
unsafe_allow_html=True,
)
# Button to clear data
if st.button("Clear Data"):
# Reset the data and write to the file
saved_data = []
shared_data = {'buttonCleared': True}
# Add a timestamp to the button press and cleared data
timestamp = datetime.now().isoformat()
entry = {"timestamp": timestamp, "data": shared_data}
# Save the new entry to the file
saved_data.append(entry)
with open(DATA_FILE, "w") as file:
json.dump(saved_data, file, indent=4)
st.success(f"Data cleared at {timestamp}")
# Display received requests
st.write("Most Recent GET Requests (updates every second):")
data_container = st.empty()
# Continuously update the displayed data
while True:
# Load the saved data to ensure persistence across sessions
if os.path.exists(DATA_FILE):
with open(DATA_FILE, "r") as file:
saved_data = json.load(file)
# Display the latest 5 entries
with data_container.container():
st.markdown(
f"""
<div class="blue-box">
<pre>{json.dumps(saved_data[-5:], indent=4)}</pre>
</div>
""",
unsafe_allow_html=True,
)
time.sleep(1)