Spaces:
Sleeping
Sleeping
eaglelandsonce
commited on
Commit
•
93cfcaa
1
Parent(s):
68aba08
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
|
3 |
# Set up the layout of the app
|
4 |
st.title("Intro to Python: Interactive Learning")
|
@@ -9,6 +11,24 @@ def create_explanation(title, content):
|
|
9 |
with st.expander(f"Explanation: {title}"):
|
10 |
st.markdown(content)
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# 1. Basic Syntax and Variables
|
13 |
st.subheader("1. Basic Syntax and Variables")
|
14 |
code1 = '''name = "Alice"
|
@@ -33,12 +53,12 @@ st.write("### Try it Yourself:")
|
|
33 |
user_code1 = st.text_area("Example 1: Paste or modify the code and hit 'Run' to see the output.", value=code1, height=150, key="1")
|
34 |
|
35 |
if st.button("Run Example 1 Code"):
|
36 |
-
|
37 |
-
|
38 |
-
exec(user_code1, {}, exec_locals)
|
39 |
st.success("Code ran successfully!")
|
40 |
-
|
41 |
-
|
|
|
42 |
|
43 |
# 2. Control Flow: If-Else Statements
|
44 |
st.subheader("2. Control Flow: If-Else Statements")
|
@@ -65,12 +85,12 @@ st.write("### Try it Yourself:")
|
|
65 |
user_code2 = st.text_area("Example 2: Paste or modify the code and hit 'Run' to see the output.", value=code2, height=150, key="2")
|
66 |
|
67 |
if st.button("Run Example 2 Code"):
|
68 |
-
|
69 |
-
|
70 |
-
exec(user_code2, {}, exec_locals)
|
71 |
st.success("Code ran successfully!")
|
72 |
-
|
73 |
-
|
|
|
74 |
|
75 |
# 3. Loops: For and While
|
76 |
st.subheader("3. Loops: For and While")
|
@@ -98,12 +118,12 @@ st.write("### Try it Yourself:")
|
|
98 |
user_code3 = st.text_area("Example 3: Paste or modify the code and hit 'Run' to see the output.", value=code3, height=150, key="3")
|
99 |
|
100 |
if st.button("Run Example 3 Code"):
|
101 |
-
|
102 |
-
|
103 |
-
exec(user_code3, {}, exec_locals)
|
104 |
st.success("Code ran successfully!")
|
105 |
-
|
106 |
-
|
|
|
107 |
|
108 |
# 4. Functions: Defining and Using Functions
|
109 |
st.subheader("4. Functions: Defining and Using Functions")
|
@@ -129,12 +149,12 @@ st.write("### Try it Yourself:")
|
|
129 |
user_code4 = st.text_area("Example 4: Paste or modify the code and hit 'Run' to see the output.", value=code4, height=150, key="4")
|
130 |
|
131 |
if st.button("Run Example 4 Code"):
|
132 |
-
|
133 |
-
|
134 |
-
exec(user_code4, {}, exec_locals)
|
135 |
st.success("Code ran successfully!")
|
136 |
-
|
137 |
-
|
|
|
138 |
|
139 |
# 5. Lists and Dictionaries
|
140 |
st.subheader("5. Basic Data Structures: Lists and Dictionaries")
|
@@ -159,12 +179,12 @@ st.write("### Try it Yourself:")
|
|
159 |
user_code5 = st.text_area("Example 5: Paste or modify the code and hit 'Run' to see the output.", value=code5, height=150, key="5")
|
160 |
|
161 |
if st.button("Run Example 5 Code"):
|
162 |
-
|
163 |
-
|
164 |
-
exec(user_code5, {}, exec_locals)
|
165 |
st.success("Code ran successfully!")
|
166 |
-
|
167 |
-
|
|
|
168 |
|
169 |
# Footer
|
170 |
st.write("This app is an educational tool to help you learn basic Python concepts. Keep exploring and practicing to master the language!")
|
|
|
1 |
import streamlit as st
|
2 |
+
import io
|
3 |
+
import sys
|
4 |
|
5 |
# Set up the layout of the app
|
6 |
st.title("Intro to Python: Interactive Learning")
|
|
|
11 |
with st.expander(f"Explanation: {title}"):
|
12 |
st.markdown(content)
|
13 |
|
14 |
+
# Helper function to execute code and capture output
|
15 |
+
def run_code(user_code):
|
16 |
+
# Create StringIO object to capture output
|
17 |
+
output = io.StringIO()
|
18 |
+
# Redirect stdout to capture print statements
|
19 |
+
sys.stdout = output
|
20 |
+
try:
|
21 |
+
# Execute the user's code
|
22 |
+
exec(user_code, {})
|
23 |
+
# Return the captured output
|
24 |
+
result = output.getvalue()
|
25 |
+
return result, None
|
26 |
+
except Exception as e:
|
27 |
+
return None, str(e)
|
28 |
+
finally:
|
29 |
+
# Reset stdout
|
30 |
+
sys.stdout = sys.__stdout__
|
31 |
+
|
32 |
# 1. Basic Syntax and Variables
|
33 |
st.subheader("1. Basic Syntax and Variables")
|
34 |
code1 = '''name = "Alice"
|
|
|
53 |
user_code1 = st.text_area("Example 1: Paste or modify the code and hit 'Run' to see the output.", value=code1, height=150, key="1")
|
54 |
|
55 |
if st.button("Run Example 1 Code"):
|
56 |
+
result, error = run_code(user_code1)
|
57 |
+
if result:
|
|
|
58 |
st.success("Code ran successfully!")
|
59 |
+
st.text(result)
|
60 |
+
elif error:
|
61 |
+
st.error(f"Error: {error}")
|
62 |
|
63 |
# 2. Control Flow: If-Else Statements
|
64 |
st.subheader("2. Control Flow: If-Else Statements")
|
|
|
85 |
user_code2 = st.text_area("Example 2: Paste or modify the code and hit 'Run' to see the output.", value=code2, height=150, key="2")
|
86 |
|
87 |
if st.button("Run Example 2 Code"):
|
88 |
+
result, error = run_code(user_code2)
|
89 |
+
if result:
|
|
|
90 |
st.success("Code ran successfully!")
|
91 |
+
st.text(result)
|
92 |
+
elif error:
|
93 |
+
st.error(f"Error: {error}")
|
94 |
|
95 |
# 3. Loops: For and While
|
96 |
st.subheader("3. Loops: For and While")
|
|
|
118 |
user_code3 = st.text_area("Example 3: Paste or modify the code and hit 'Run' to see the output.", value=code3, height=150, key="3")
|
119 |
|
120 |
if st.button("Run Example 3 Code"):
|
121 |
+
result, error = run_code(user_code3)
|
122 |
+
if result:
|
|
|
123 |
st.success("Code ran successfully!")
|
124 |
+
st.text(result)
|
125 |
+
elif error:
|
126 |
+
st.error(f"Error: {error}")
|
127 |
|
128 |
# 4. Functions: Defining and Using Functions
|
129 |
st.subheader("4. Functions: Defining and Using Functions")
|
|
|
149 |
user_code4 = st.text_area("Example 4: Paste or modify the code and hit 'Run' to see the output.", value=code4, height=150, key="4")
|
150 |
|
151 |
if st.button("Run Example 4 Code"):
|
152 |
+
result, error = run_code(user_code4)
|
153 |
+
if result:
|
|
|
154 |
st.success("Code ran successfully!")
|
155 |
+
st.text(result)
|
156 |
+
elif error:
|
157 |
+
st.error(f"Error: {error}")
|
158 |
|
159 |
# 5. Lists and Dictionaries
|
160 |
st.subheader("5. Basic Data Structures: Lists and Dictionaries")
|
|
|
179 |
user_code5 = st.text_area("Example 5: Paste or modify the code and hit 'Run' to see the output.", value=code5, height=150, key="5")
|
180 |
|
181 |
if st.button("Run Example 5 Code"):
|
182 |
+
result, error = run_code(user_code5)
|
183 |
+
if result:
|
|
|
184 |
st.success("Code ran successfully!")
|
185 |
+
st.text(result)
|
186 |
+
elif error:
|
187 |
+
st.error(f"Error: {error}")
|
188 |
|
189 |
# Footer
|
190 |
st.write("This app is an educational tool to help you learn basic Python concepts. Keep exploring and practicing to master the language!")
|