Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,71 @@ from contextlib import redirect_stdout
|
|
5 |
import re
|
6 |
import mistune
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def extract_python_code(markdown_text):
|
9 |
"""Extract Python code blocks from markdown text."""
|
10 |
pattern = r"```python\s*(.*?)\s*```"
|
@@ -40,51 +105,68 @@ def main():
|
|
40 |
st.markdown("Enter Python code directly or upload a .py/.md file")
|
41 |
|
42 |
# File uploader
|
43 |
-
uploaded_file = st.file_uploader("Upload a Python (.py) or Markdown (.md) file",
|
44 |
type=['py', 'md'])
|
45 |
|
|
|
|
|
|
|
|
|
46 |
# Code input area
|
47 |
if uploaded_file is None:
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
51 |
else:
|
52 |
content = uploaded_file.getvalue().decode()
|
53 |
if uploaded_file.type == "text/markdown":
|
54 |
# Extract Python code from markdown
|
55 |
code_blocks = extract_python_code(content)
|
56 |
if code_blocks:
|
57 |
-
|
58 |
else:
|
59 |
-
st.error("No Python code blocks found in the markdown file!")
|
60 |
return
|
61 |
else:
|
62 |
# Assume it's a Python file
|
63 |
-
|
64 |
|
65 |
-
st.code(
|
|
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
if
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
77 |
else:
|
78 |
-
st.
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
81 |
|
82 |
# Add some usage information
|
83 |
with st.expander("ℹ️ How to use"):
|
84 |
st.markdown("""
|
85 |
1. Either upload a Python (.py) or Markdown (.md) file containing Python code
|
86 |
2. Or enter Python code directly in the text area
|
87 |
-
3.
|
|
|
|
|
88 |
|
89 |
**Note:** For markdown files, the code should be in Python code blocks like:
|
90 |
````
|
|
|
5 |
import re
|
6 |
import mistune
|
7 |
|
8 |
+
# Default example code
|
9 |
+
DEFAULT_CODE = '''import streamlit as st
|
10 |
+
import random
|
11 |
+
|
12 |
+
# Set page config
|
13 |
+
st.set_page_config(page_title="Test App", page_icon="🧪")
|
14 |
+
|
15 |
+
# Title and introduction
|
16 |
+
st.title("🧪 Test Program")
|
17 |
+
st.markdown("Welcome to this simple test application!")
|
18 |
+
|
19 |
+
# Create two columns
|
20 |
+
col1, col2 = st.columns(2)
|
21 |
+
|
22 |
+
with col1:
|
23 |
+
# Number input
|
24 |
+
number = st.number_input("Enter a number:", min_value=1, max_value=100, value=50)
|
25 |
+
|
26 |
+
# Slider
|
27 |
+
multiplier = st.slider("Select multiplier:", 1, 10, 5)
|
28 |
+
|
29 |
+
# Calculate and display result
|
30 |
+
if st.button("Calculate"):
|
31 |
+
result = number * multiplier
|
32 |
+
st.success(f"Result: {result}")
|
33 |
+
|
34 |
+
with col2:
|
35 |
+
# Color picker
|
36 |
+
color = st.color_picker("Pick a color", "#00f900")
|
37 |
+
|
38 |
+
# Display a box with the selected color
|
39 |
+
st.markdown(
|
40 |
+
f"""
|
41 |
+
<div style="background-color: {color}; padding: 20px; border-radius: 5px;">
|
42 |
+
Selected color: {color}
|
43 |
+
</div>
|
44 |
+
""",
|
45 |
+
unsafe_allow_html=True
|
46 |
+
)
|
47 |
+
|
48 |
+
# Random number generator
|
49 |
+
if st.button("Generate Random Number"):
|
50 |
+
random_num = random.randint(1, 100)
|
51 |
+
st.balloons()
|
52 |
+
st.write(f"Random number: {random_num}")
|
53 |
+
|
54 |
+
# Add a text input with a toggle
|
55 |
+
show_text_input = st.toggle("Show text input")
|
56 |
+
if show_text_input:
|
57 |
+
user_text = st.text_input("Enter some text:")
|
58 |
+
if user_text:
|
59 |
+
st.write("You entered:", user_text)
|
60 |
+
|
61 |
+
# Add an expander with additional information
|
62 |
+
with st.expander("About this app"):
|
63 |
+
st.write("""
|
64 |
+
This is a simple test application demonstrating various Streamlit features:
|
65 |
+
- Number inputs and sliders
|
66 |
+
- Calculations
|
67 |
+
- Color picker
|
68 |
+
- Random number generation
|
69 |
+
- Text input with toggle
|
70 |
+
- Columns layout
|
71 |
+
""")'''
|
72 |
+
|
73 |
def extract_python_code(markdown_text):
|
74 |
"""Extract Python code blocks from markdown text."""
|
75 |
pattern = r"```python\s*(.*?)\s*```"
|
|
|
105 |
st.markdown("Enter Python code directly or upload a .py/.md file")
|
106 |
|
107 |
# File uploader
|
108 |
+
uploaded_file = st.file_uploader("📤 Upload a Python (.py) or Markdown (.md) file",
|
109 |
type=['py', 'md'])
|
110 |
|
111 |
+
# Initialize session state for code if not exists
|
112 |
+
if 'code' not in st.session_state:
|
113 |
+
st.session_state.code = DEFAULT_CODE
|
114 |
+
|
115 |
# Code input area
|
116 |
if uploaded_file is None:
|
117 |
+
code_input = st.text_area("💻 Python Code Editor:",
|
118 |
+
value=st.session_state.code,
|
119 |
+
height=400,
|
120 |
+
key="code_editor")
|
121 |
else:
|
122 |
content = uploaded_file.getvalue().decode()
|
123 |
if uploaded_file.type == "text/markdown":
|
124 |
# Extract Python code from markdown
|
125 |
code_blocks = extract_python_code(content)
|
126 |
if code_blocks:
|
127 |
+
code_input = code_blocks[0] # Use the first Python code block found
|
128 |
else:
|
129 |
+
st.error("❌ No Python code blocks found in the markdown file!")
|
130 |
return
|
131 |
else:
|
132 |
# Assume it's a Python file
|
133 |
+
code_input = content
|
134 |
|
135 |
+
st.code(code_input, language='python')
|
136 |
+
|
137 |
+
# Button columns
|
138 |
+
col1, col2, col3 = st.columns([1, 1, 4])
|
139 |
|
140 |
+
with col1:
|
141 |
+
# Execute button
|
142 |
+
if st.button("▶️ Run Code"):
|
143 |
+
if code_input:
|
144 |
+
st.markdown("### 📋 Output:")
|
145 |
+
output, error = execute_code(code_input)
|
146 |
+
|
147 |
+
if error:
|
148 |
+
st.error(f"❌ Error:\n{error}")
|
149 |
+
elif output:
|
150 |
+
st.code(output)
|
151 |
+
else:
|
152 |
+
st.success("✅ Code executed successfully with no output.")
|
153 |
else:
|
154 |
+
st.warning("⚠️ Please enter some code to execute!")
|
155 |
+
|
156 |
+
with col2:
|
157 |
+
# Clear button
|
158 |
+
if st.button("🗑️ Clear Code"):
|
159 |
+
st.session_state.code = ""
|
160 |
+
st.rerun()
|
161 |
|
162 |
# Add some usage information
|
163 |
with st.expander("ℹ️ How to use"):
|
164 |
st.markdown("""
|
165 |
1. Either upload a Python (.py) or Markdown (.md) file containing Python code
|
166 |
2. Or enter Python code directly in the text area
|
167 |
+
3. Use the buttons below the editor to:
|
168 |
+
- ▶️ Run Code: Execute the current code
|
169 |
+
- 🗑️ Clear Code: Clear the editor
|
170 |
|
171 |
**Note:** For markdown files, the code should be in Python code blocks like:
|
172 |
````
|