File size: 1,397 Bytes
3370f55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
857469a
 
c0abdc4
 
857469a
3370f55
 
 
 
 
 
 
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
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")