Spaces:
Sleeping
Sleeping
import streamlit as st | |
import base64 | |
import os | |
# Read existing app.py | |
with open('app.py', 'r') as f: | |
app_code = f.read() | |
# Streamlit UI | |
st.title("Self-Modifying Streamlit App") | |
# Textbox for username | |
username = st.text_input("Enter your username:", "anonymous") | |
# File Upload | |
uploaded_file = st.file_uploader("Choose a file") | |
if uploaded_file is not None: | |
file_content = uploaded_file.read() | |
# Convert file to base64 | |
b64 = base64.b64encode(file_content).decode() | |
st.markdown(f"File successfully uploaded. Here is the download link:") | |
# Create a download link for the uploaded file | |
href = f'<a href="data:file/txt;base64,{b64}" download="your_file.txt">Download Uploaded File</a>' | |
st.markdown(href, unsafe_allow_html=True) | |
# Modify app.py | |
new_code = f"# Modified by {username}\n" | |
modified_app_code = app_code + "\n" + new_code | |
with open('app.py', 'w') as f: | |
f.write(modified_app_code) | |
# Display the new code in a textbox | |
st.text_area("Newly Modified app.py Code:", modified_app_code) | |
# Convert modified app.py to base64 for download | |
app_b64 = base64.b64encode(modified_app_code.encode()).decode() | |
app_href = f'<a href="data:file/py;base64,{app_b64}" download="modified_app.py">Download Modified app.py</a>' | |
st.markdown(app_href, unsafe_allow_html=True) | |
# Refresh app | |
os.system("streamlit rerun") | |