Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,23 @@
|
|
1 |
import streamlit as st
|
2 |
import base64
|
3 |
import os
|
|
|
4 |
|
5 |
-
|
6 |
-
with open('app.py', 'r') as f:
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Streamlit UI
|
10 |
st.title("Self-Modifying Streamlit App")
|
@@ -14,30 +27,25 @@ username = st.text_input("Enter your username:", "anonymous")
|
|
14 |
|
15 |
# File Upload
|
16 |
uploaded_file = st.file_uploader("Choose a file")
|
|
|
17 |
if uploaded_file is not None:
|
18 |
file_content = uploaded_file.read()
|
|
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
# Create a download link for the uploaded file
|
25 |
-
href = f'<a href="data:file/txt;base64,{b64}" download="your_file.txt">Download Uploaded File</a>'
|
26 |
-
st.markdown(href, unsafe_allow_html=True)
|
27 |
-
|
28 |
-
# Modify app.py
|
29 |
-
new_code = f"# Modified by {username}\n"
|
30 |
modified_app_code = app_code + "\n" + new_code
|
31 |
-
|
32 |
-
|
33 |
|
34 |
# Display the new code in a textbox
|
35 |
st.text_area("Newly Modified app.py Code:", modified_app_code)
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
st.markdown(app_href, unsafe_allow_html=True)
|
41 |
|
42 |
# Refresh app
|
43 |
os.system("streamlit rerun")
|
|
|
1 |
import streamlit as st
|
2 |
import base64
|
3 |
import os
|
4 |
+
from datetime import datetime
|
5 |
|
6 |
+
def read_app_code():
|
7 |
+
with open('app.py', 'r') as f:
|
8 |
+
return f.read()
|
9 |
+
|
10 |
+
def write_app_code(modified_code):
|
11 |
+
with open('app.py', 'w') as f:
|
12 |
+
f.write(modified_code)
|
13 |
+
|
14 |
+
def get_timestamp():
|
15 |
+
return datetime.now().strftime("%Y%m%d_%H%M%S")
|
16 |
+
|
17 |
+
def create_download_link(file_content, filename):
|
18 |
+
b64 = base64.b64encode(file_content).decode()
|
19 |
+
href = f'<a href="data:file/txt;base64,{b64}" download="{filename}">Download {filename}</a>'
|
20 |
+
st.markdown(href, unsafe_allow_html=True)
|
21 |
|
22 |
# Streamlit UI
|
23 |
st.title("Self-Modifying Streamlit App")
|
|
|
27 |
|
28 |
# File Upload
|
29 |
uploaded_file = st.file_uploader("Choose a file")
|
30 |
+
|
31 |
if uploaded_file is not None:
|
32 |
file_content = uploaded_file.read()
|
33 |
+
create_download_link(file_content, "your_file.txt")
|
34 |
|
35 |
+
# Read and Modify app.py
|
36 |
+
timestamp = get_timestamp()
|
37 |
+
app_code = read_app_code()
|
38 |
+
new_code = f"# Modified by {username} on {timestamp}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
modified_app_code = app_code + "\n" + new_code
|
40 |
+
|
41 |
+
write_app_code(modified_app_code)
|
42 |
|
43 |
# Display the new code in a textbox
|
44 |
st.text_area("Newly Modified app.py Code:", modified_app_code)
|
45 |
|
46 |
+
# Create download link for modified app.py
|
47 |
+
download_filename = f"modified_app_{timestamp}.py"
|
48 |
+
create_download_link(modified_app_code.encode(), download_filename)
|
|
|
49 |
|
50 |
# Refresh app
|
51 |
os.system("streamlit rerun")
|