File size: 1,495 Bytes
3370f55
 
 
ffaab9d
3370f55
ffaab9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3370f55
 
 
 
 
 
 
 
 
ffaab9d
3370f55
 
ffaab9d
3370f55
ffaab9d
 
 
 
3370f55
ffaab9d
 
3370f55
c0abdc4
 
 
ffaab9d
 
 
c0abdc4
3370f55
 
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
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")