awacke1 commited on
Commit
ffaab9d
·
1 Parent(s): c0abdc4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -19
app.py CHANGED
@@ -1,10 +1,23 @@
1
  import streamlit as st
2
  import base64
3
  import os
 
4
 
5
- # Read existing app.py
6
- with open('app.py', 'r') as f:
7
- app_code = f.read()
 
 
 
 
 
 
 
 
 
 
 
 
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
- # Convert file to base64
21
- b64 = base64.b64encode(file_content).decode()
22
- st.markdown(f"File successfully uploaded. Here is the download link:")
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
- with open('app.py', 'w') as f:
32
- f.write(modified_app_code)
33
 
34
  # Display the new code in a textbox
35
  st.text_area("Newly Modified app.py Code:", modified_app_code)
36
 
37
- # Convert modified app.py to base64 for download
38
- app_b64 = base64.b64encode(modified_app_code.encode()).decode()
39
- app_href = f'<a href="data:file/py;base64,{app_b64}" download="modified_app.py">Download Modified app.py</a>'
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")