Skip to content

Commit b0c20d6

Browse files
committed
Adding 11th Project
1 parent 06728df commit b0c20d6

File tree

1 file changed

+129
-0
lines changed
  • Project 11 - Python Code Editor

1 file changed

+129
-0
lines changed
+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
from pywebio.input import textarea, select, input, NUMBER
2+
from pywebio.output import put_html, put_table, put_text, use_scope, put_error, put_code, put_button, clear
3+
from pywebio import start_server
4+
import io
5+
import sys
6+
from pywebio import session
7+
8+
def execute_code(code):
9+
o_stdout = sys.stdout
10+
o_stderr = sys.stderr
11+
12+
redirected_stdout = sys.stdout = io.StringIO()
13+
redirected_stderr = sys.stderr = io.StringIO()
14+
15+
try:
16+
exec_global = {}
17+
exec(code, exec_global)
18+
return redirected_stdout.getvalue(), redirected_stderr.getvalue()
19+
except Exception as e:
20+
return "", f"Error Executing Code: {e}"
21+
finally:
22+
sys.stdout = o_stdout
23+
sys.stderr = o_stderr
24+
25+
def customization():
26+
# Real-world VS Code themes
27+
theme_options = [
28+
"default dark+",
29+
"default light+",
30+
"quiet light",
31+
"solarized dark",
32+
"solarized light",
33+
"monokai",
34+
"dark (Visual Studio)",
35+
"light (Visual Studio)",
36+
"dark+ (Contrast)",
37+
"kimbie dark",
38+
"darcula",
39+
40+
]
41+
42+
# Font families
43+
font_options = [
44+
"Ubuntu Mono",
45+
"Fira Code",
46+
"monospace",
47+
"Consolas",
48+
"Roboto Mono",
49+
"Inconsolata",
50+
"Source Code Pro",
51+
"Courier New",
52+
"Hack",
53+
"Cascadia Code"
54+
]
55+
56+
57+
58+
theme = select("Select Theme", options=theme_options)
59+
font = select("Select Font Family", options=font_options)
60+
font_num = input('Select Font Size', type=NUMBER, placeholder='Default 18px')
61+
62+
return theme, font, font_num
63+
64+
65+
66+
def main():
67+
put_html("""
68+
<style>
69+
.app-title {
70+
font-size: 36px; /* Adjust font size as desired */
71+
font-weight: bold;
72+
color: #247ba0; /* Set your preferred color */
73+
text-align: center; /* Center alignment */
74+
margin-bottom: 20px; /* Add some margin below */
75+
}
76+
</style>
77+
<h1 class="app-title">Python Code Editor</h1>
78+
""")
79+
80+
theme_name, font_family, font_size = customization()
81+
82+
put_html(f"""
83+
<style>
84+
.CodeMirror {{
85+
font-size: {font_size}px !important;
86+
font-family: '{font_family}', monospace;
87+
line-height: 1.5;
88+
background-color: var(--vscode-{theme_name.replace(' ', '').lower()}-background);
89+
color: var(--vscode-{theme_name.replace(' ', '').lower()}-foreground);
90+
}}
91+
</style>
92+
""")
93+
94+
c_history = {}
95+
96+
while True:
97+
code = textarea('Enter Python code', code={
98+
'rows': 20,
99+
'required': True,
100+
'mode': 'python',
101+
'theme': f'{theme_name}',
102+
'placeholder': 'Type your Python code here'
103+
})
104+
105+
if code.strip():
106+
if code not in c_history:
107+
c_history[code] = (None, None)
108+
stdout, stderr = execute_code(code)
109+
c_history[code] = (stdout, stderr)
110+
else:
111+
stdout, stderr = c_history[code]
112+
113+
with use_scope('output', clear=True):
114+
put_html("<b>Source Code:</b>")
115+
put_code(code, language='python')
116+
if stdout:
117+
put_html("<b>Standard Output:</b>")
118+
put_text(stdout)
119+
if stderr:
120+
put_html("<b>Standard Error:</b>")
121+
put_error(stderr)
122+
123+
if stdout or stderr:
124+
put_button('Clear', onclick=lambda: clear(scope='output'))
125+
else:
126+
put_html("<b>Error:</b> Please enter some Python code.")
127+
128+
# Start the server
129+
start_server(main, port=8080)

0 commit comments

Comments
 (0)