eaglelandsonce commited on
Commit
93cfcaa
1 Parent(s): 68aba08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -25
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
- try:
37
- exec_locals = {}
38
- exec(user_code1, {}, exec_locals)
39
  st.success("Code ran successfully!")
40
- except Exception as e:
41
- st.error(f"Error: {str(e)}")
 
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
- try:
69
- exec_locals = {}
70
- exec(user_code2, {}, exec_locals)
71
  st.success("Code ran successfully!")
72
- except Exception as e:
73
- st.error(f"Error: {str(e)}")
 
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
- try:
102
- exec_locals = {}
103
- exec(user_code3, {}, exec_locals)
104
  st.success("Code ran successfully!")
105
- except Exception as e:
106
- st.error(f"Error: {str(e)}")
 
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
- try:
133
- exec_locals = {}
134
- exec(user_code4, {}, exec_locals)
135
  st.success("Code ran successfully!")
136
- except Exception as e:
137
- st.error(f"Error: {str(e)}")
 
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
- try:
163
- exec_locals = {}
164
- exec(user_code5, {}, exec_locals)
165
  st.success("Code ran successfully!")
166
- except Exception as e:
167
- st.error(f"Error: {str(e)}")
 
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!")