-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget_code_cell.py
More file actions
103 lines (86 loc) · 2.78 KB
/
Copy pathwidget_code_cell.py
File metadata and controls
103 lines (86 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from IPython.utils.capture import capture_output
import inspect
import ipywidgets
import ast
from IPython.display import display
import traceback
def execute_with_last_expr(code, global_ns):
"""
Executes code and returns the value of the last expression (if any).
"""
tree = ast.parse(code)
# If last node is an expression, separate it
if isinstance(tree.body[-1], ast.Expr):
last_expr = ast.Expression(tree.body[-1].value)
exec(compile(ast.Module(tree.body[:-1], []), "<cell>", "exec"), global_ns)
return eval(compile(last_expr, "<cell>", "eval"), global_ns)
else:
exec(code, global_ns)
return None
def make_code_cell(initial_text:str="", placeholder:str="Write Python code here...\n\n", width:str="95%", height:str='200px', global_ns=None):
"""
Creates a mini notebook-like code cell:
- Text area for Python code
- Run button
- Output area
"""
if global_ns is None:
frame = inspect.currentframe()
try:
if frame is not None and frame.f_back is not None:
caller_frame = frame.f_back
global_ns = dict(caller_frame.f_globals)
global_ns.update(caller_frame.f_locals)
else:
global_ns = globals()
finally:
del frame
css = ipywidgets.HTML("""
<style>
textarea {
font-family: monospace !important;
font-size: 14px;
}
</style>
""")
code_area = ipywidgets.Textarea(
value=initial_text,
placeholder=placeholder,
layout=ipywidgets.Layout(width=width, height=height),
style={"font_family": "monospace"}
)
run_button = ipywidgets.Button(
description="Run",
button_style="success",
tooltip="Execute the code",
icon="play"
)
output = ipywidgets.Output(layout=ipywidgets.Layout(border="1px solid #ccc", padding='6px', width=width))
def run_code(_):
output.clear_output()
run_button.disabled = True
run_button.button_style = "warning"
run_button.icon = "spinner"
code = code_area.value
try:
with output:
# exec(code, EXEC_GLOBALS)
result = execute_with_last_expr(code, global_ns)
if result is not None:
display(result)
except Exception:
with output:
traceback.print_exc()
finally:
run_button.disabled = False
run_button.button_style = "success"
run_button.icon = "play"
run_button.on_click(run_code)
return ipywidgets.VBox([
ipywidgets.HTML("<b>Python Code Cell</b>"),
css,
code_area,
run_button,
output
])
# code_cell = make_code_cell()