awacke1's picture
Update app.py
ffaab9d
import streamlit as st
import base64
import os
from datetime import datetime
def read_app_code():
with open('app.py', 'r') as f:
return f.read()
def write_app_code(modified_code):
with open('app.py', 'w') as f:
f.write(modified_code)
def get_timestamp():
return datetime.now().strftime("%Y%m%d_%H%M%S")
def create_download_link(file_content, filename):
b64 = base64.b64encode(file_content).decode()
href = f'<a href="data:file/txt;base64,{b64}" download="{filename}">Download {filename}</a>'
st.markdown(href, unsafe_allow_html=True)
# 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()
create_download_link(file_content, "your_file.txt")
# Read and Modify app.py
timestamp = get_timestamp()
app_code = read_app_code()
new_code = f"# Modified by {username} on {timestamp}\n"
modified_app_code = app_code + "\n" + new_code
write_app_code(modified_app_code)
# Display the new code in a textbox
st.text_area("Newly Modified app.py Code:", modified_app_code)
# Create download link for modified app.py
download_filename = f"modified_app_{timestamp}.py"
create_download_link(modified_app_code.encode(), download_filename)
# Refresh app
os.system("streamlit rerun")