Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,148 +1,71 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
state
|
6 |
-
if '
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
st.
|
15 |
|
16 |
-
if
|
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 |
-
javascript_code = """
|
52 |
-
<script>
|
53 |
-
// Function to return a string
|
54 |
-
function myJavaScriptFunction() {
|
55 |
-
return "Hello from JavaScript!";
|
56 |
-
}
|
57 |
-
|
58 |
-
// Function to call and send data back to Python
|
59 |
-
function callAndCapture() {
|
60 |
-
const result = myJavaScriptFunction();
|
61 |
-
// Send the result back to Streamlit
|
62 |
-
window.parent.postMessage({ isStreamlitMessage: true, type: 'streamlit:js_return', data: result }, '*');
|
63 |
-
}
|
64 |
-
|
65 |
-
// Add an event listener to listen for messages from Streamlit
|
66 |
-
window.addEventListener('message', function(event) {
|
67 |
-
if (event.data && event.data.type === 'streamlit:buttonClick') {
|
68 |
-
callAndCapture();
|
69 |
-
}
|
70 |
-
});
|
71 |
-
</script>
|
72 |
-
<button onclick="callAndCapture()">Run JavaScript Function</button>
|
73 |
-
"""
|
74 |
-
|
75 |
-
# Embed JavaScript into Streamlit app
|
76 |
-
html(javascript_code, height=300)
|
77 |
-
|
78 |
-
# Placeholder to display results
|
79 |
-
result_placeholder = st.empty()
|
80 |
-
|
81 |
-
# Add Streamlit button to trigger JavaScript function
|
82 |
-
if st.button("Call JavaScript Function"):
|
83 |
-
st.write("Waiting for JavaScript response...")
|
84 |
-
trigger_js = """
|
85 |
-
<script>
|
86 |
-
// Trigger the JavaScript function via a postMessage event
|
87 |
-
window.parent.postMessage({ type: 'streamlit:buttonClick' }, '*');
|
88 |
-
</script>
|
89 |
-
"""
|
90 |
-
html(trigger_js, height=0)
|
91 |
-
|
92 |
-
# Add a listener for JavaScript responses
|
93 |
-
st.markdown(
|
94 |
-
"""
|
95 |
-
<script>
|
96 |
-
// Listen for messages from JavaScript
|
97 |
-
window.addEventListener('message', (event) => {
|
98 |
-
if (event.data && event.data.type === 'streamlit:js_return') {
|
99 |
-
// Pass the data back to Streamlit as a new message
|
100 |
-
const result = event.data.data;
|
101 |
-
document.dispatchEvent(new CustomEvent('StreamlitResult', { detail: result }));
|
102 |
-
}
|
103 |
-
});
|
104 |
-
|
105 |
-
// Dispatch data from CustomEvent to Streamlit
|
106 |
-
document.addEventListener('StreamlitResult', (e) => {
|
107 |
-
const data = e.detail;
|
108 |
-
window.parent.postMessage({ type: 'streamlit:result', data: data }, '*');
|
109 |
-
});
|
110 |
-
</script>
|
111 |
-
""",
|
112 |
-
unsafe_allow_html=True,
|
113 |
-
)
|
114 |
-
|
115 |
-
# Use an iframe for communication
|
116 |
-
if "js_result" not in st.session_state:
|
117 |
-
st.session_state["js_result"] = ""
|
118 |
-
|
119 |
-
try:
|
120 |
-
# Listen for result data from JavaScript
|
121 |
-
received_result = html(
|
122 |
-
"""
|
123 |
-
<script>
|
124 |
-
window.addEventListener('message', (event) => {
|
125 |
-
if (event.data && event.data.type === 'streamlit:result') {
|
126 |
-
const data = event.data.data;
|
127 |
-
// Send the result back to Streamlit
|
128 |
-
document.dispatchEvent(new CustomEvent('PythonReceivedResult', { detail: data }));
|
129 |
-
}
|
130 |
-
});
|
131 |
-
|
132 |
-
document.addEventListener('PythonReceivedResult', (e) => {
|
133 |
-
const received = e.detail;
|
134 |
-
document.getElementById('result-placeholder').innerText = received;
|
135 |
-
});
|
136 |
-
</script>
|
137 |
-
<div id="result-placeholder"></div>
|
138 |
-
""",
|
139 |
-
height=0,
|
140 |
-
)
|
141 |
-
|
142 |
-
# If there's a new result, update it in session state
|
143 |
-
if received_result and received_result != st.session_state["js_result"]:
|
144 |
-
st.session_state["js_result"] = received_result
|
145 |
-
result_placeholder.write(f"Result from JavaScript: {received_result}")
|
146 |
-
|
147 |
-
except Exception as e:
|
148 |
-
st.error(f"An error occurred: {e}")
|
|
|
1 |
import streamlit as st
|
2 |
+
from pathlib import Path
|
3 |
+
import json
|
4 |
+
|
5 |
+
# π¦ Magical state initialization
|
6 |
+
if 'file_data' not in st.session_state:
|
7 |
+
st.session_state.file_data = {}
|
8 |
+
|
9 |
+
def save_to_file(data, filename):
|
10 |
+
"""π Rainbow file saver - spreading joy through data persistence!"""
|
11 |
+
try:
|
12 |
+
with open(filename, 'w') as f:
|
13 |
+
json.dump(data, f)
|
14 |
+
return True
|
15 |
+
except Exception as e:
|
16 |
+
st.error(f"Oopsie! π Couldn't save to {filename}: {str(e)}")
|
17 |
+
return False
|
18 |
+
|
19 |
+
def load_from_file(filename):
|
20 |
+
"""π Pretty file loader - bringing your data back with style!"""
|
21 |
+
try:
|
22 |
+
with open(filename, 'r') as f:
|
23 |
+
return json.load(f)
|
24 |
+
except FileNotFoundError:
|
25 |
+
st.info(f"β¨ New adventure! No previous data found in {filename}")
|
26 |
+
return {}
|
27 |
+
except json.JSONDecodeError:
|
28 |
+
st.warning(f"π Hmm, the file {filename} contains invalid JSON. Starting fresh!")
|
29 |
+
return {}
|
30 |
+
|
31 |
+
def main():
|
32 |
+
st.title("π Super Fun File Handler!")
|
33 |
|
34 |
+
# π¨ Pretty file upload section
|
35 |
+
uploaded_file = st.file_uploader("Upload your happy little file!", type=['txt', 'json'])
|
36 |
|
37 |
+
if uploaded_file:
|
38 |
+
# πͺ Process the uploaded file with circus-level excitement!
|
39 |
+
content = uploaded_file.getvalue().decode()
|
40 |
+
st.session_state.file_data[uploaded_file.name] = content
|
41 |
+
st.success(f"π Woohoo! Successfully loaded {uploaded_file.name}")
|
42 |
|
43 |
+
# π Display our magical collection of files
|
44 |
+
if st.session_state.file_data:
|
45 |
+
st.subheader("ποΈ Your Wonderful Files:")
|
46 |
+
for filename, content in st.session_state.file_data.items():
|
47 |
+
with st.expander(f"π {filename}"):
|
48 |
+
st.text_area("Content", content, height=150, key=filename)
|
49 |
+
|
50 |
+
if st.button(f"πΎ Save changes to {filename}"):
|
51 |
+
updated_content = st.session_state[filename]
|
52 |
+
if save_to_file(updated_content, filename):
|
53 |
+
st.success(f"π Yay! Saved {filename} successfully!")
|
54 |
+
|
55 |
+
# πͺ System prompt section
|
56 |
+
st.subheader("π€ Super Smart System Prompt Generator")
|
57 |
+
if st.session_state.file_data:
|
58 |
+
selected_files = st.multiselect(
|
59 |
+
"Select files for your prompt",
|
60 |
+
list(st.session_state.file_data.keys())
|
61 |
+
)
|
62 |
+
|
63 |
+
if selected_files:
|
64 |
+
prompt = "Here are the contents of the selected files:\n\n"
|
65 |
+
for file in selected_files:
|
66 |
+
prompt += f"File: {file}\n```\n{st.session_state.file_data[file]}\n```\n\n"
|
67 |
+
|
68 |
+
st.text_area("Generated Prompt", prompt, height=300)
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|