Skip to content

Commit 98866bf

Browse files
committed
add jupyter notebook able to communicate with a K3 mep server
Signed-off-by: Pierre Jeanjean <[email protected]>
1 parent 19c7c35 commit 98866bf

File tree

8 files changed

+749
-0
lines changed

8 files changed

+749
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/MEPClient-checkpoint.ipynb

python_client/MEPClient.ipynb

+224
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 5,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"%%capture\n",
10+
"%pip install websockets==8.1"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 6,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"from mep_client.k3_mep_client import K3MepClient\n",
20+
"\n",
21+
"client = K3MepClient()"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 7,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"import time\n",
31+
"\n",
32+
"def update_variables():\n",
33+
" variable_output_widget.clear_output()\n",
34+
" with variable_output_widget:\n",
35+
" print()\n",
36+
" print()\n",
37+
" print(\"Variables\")\n",
38+
" print(\"―――――――――\")\n",
39+
" client.variables()\n",
40+
" \n",
41+
"def update_stack():\n",
42+
" stack_output_widget.clear_output()\n",
43+
" with stack_output_widget:\n",
44+
" print()\n",
45+
" print()\n",
46+
" print(\"Stack Trace\")\n",
47+
" print(\"―――――――――――\")\n",
48+
" client.stack_trace()\n",
49+
" \n",
50+
"def update_model():\n",
51+
" model_output_widget.clear_output()\n",
52+
" with model_output_widget:\n",
53+
" print()\n",
54+
" print()\n",
55+
" print(\"Model\")\n",
56+
" print(\"―――――\")\n",
57+
" client.source(line_numbers=True)\n",
58+
"\n",
59+
"def connection_button_click(b):\n",
60+
" with output_widget:\n",
61+
" client.connect(host=host_widget.value, port=port_widget.value, endpoint=endpoint_widget.value)\n",
62+
" client.initialize()\n",
63+
"\n",
64+
"def launch_button_click(b):\n",
65+
" simulation_output_widget.clear_output()\n",
66+
" with simulation_output_widget:\n",
67+
" print()\n",
68+
" print()\n",
69+
" print(\"Simulation Output\")\n",
70+
" print(\"―――――――――――――――――\")\n",
71+
" client.launch(model_uri=model_uri_widget.value, model_entry_point=model_entrypoint_widget.value,\n",
72+
" init_method=init_method_widget.value, init_args=init_args_widget.value, method_entry_point=method_entrypoint_widget.value)\n",
73+
" update_model()\n",
74+
" update_stack()\n",
75+
" update_variables()\n",
76+
" \n",
77+
"def continue_button_click(b):\n",
78+
" with simulation_output_widget:\n",
79+
" client.continue_()\n",
80+
" while client.simulation_running and not client.paused:\n",
81+
" time.sleep(0.1) # Required to capture all outputs from thread\n",
82+
" update_model()\n",
83+
" update_stack()\n",
84+
" update_variables()\n",
85+
" \n",
86+
"def next_button_click(b):\n",
87+
" with simulation_output_widget:\n",
88+
" client.next()\n",
89+
" update_model()\n",
90+
" update_stack()\n",
91+
" update_variables()\n",
92+
" \n",
93+
"def step_in_button_click(b):\n",
94+
" with simulation_output_widget:\n",
95+
" client.step_in()\n",
96+
" update_model()\n",
97+
" update_stack()\n",
98+
" update_variables()\n",
99+
" \n",
100+
"def step_out_button_click(b):\n",
101+
" with simulation_output_widget:\n",
102+
" client.step_out()\n",
103+
" update_model()\n",
104+
" update_stack()\n",
105+
" update_variables()\n",
106+
" \n",
107+
"def terminate_button_click(b):\n",
108+
" with output_widget:\n",
109+
" client.terminate()\n",
110+
"\n",
111+
"def restart_button_click(b):\n",
112+
" simulation_output_widget.clear_output()\n",
113+
" with simulation_output_widget:\n",
114+
" print()\n",
115+
" print()\n",
116+
" print(\"Simulation Output\")\n",
117+
" print(\"―――――――――――――――――\")\n",
118+
" client.restart()\n",
119+
" update_model()\n",
120+
" update_stack()\n",
121+
" update_variables()\n",
122+
" \n",
123+
"def set_breakpoints_button_click(b):\n",
124+
" with output_widget:\n",
125+
" int_lines = []\n",
126+
" lines = set_breakpoints_lines_widget.value.split()\n",
127+
" for line in lines:\n",
128+
" int_lines.append(int(line))\n",
129+
" client.set_breakpoints(int_lines)"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": 8,
135+
"metadata": {},
136+
"outputs": [
137+
{
138+
"data": {
139+
"application/vnd.jupyter.widget-view+json": {
140+
"model_id": "4cdd61fd5bba4e60bf5de18ba396d368",
141+
"version_major": 2,
142+
"version_minor": 0
143+
},
144+
"text/plain": [
145+
"VBox(children=(Accordion(children=(VBox(children=(Text(value='localhost', description='Host:'), IntText(value=…"
146+
]
147+
},
148+
"metadata": {},
149+
"output_type": "display_data"
150+
}
151+
],
152+
"source": [
153+
"import ipywidgets\n",
154+
"\n",
155+
"host_widget = ipywidgets.Text(value='localhost', description='Host:')\n",
156+
"port_widget = ipywidgets.IntText(value=8090, description='Port:')\n",
157+
"endpoint_widget = ipywidgets.Text(value='mep/', description='Endpoint:')\n",
158+
"connection_button = ipywidgets.Button(description='Connect')\n",
159+
"connection_button.on_click(connection_button_click)\n",
160+
"connection_widgets = ipywidgets.VBox([host_widget, port_widget, endpoint_widget, connection_button])\n",
161+
"\n",
162+
"model_uri_widget = ipywidgets.Text(value='', description='Model URI:', layout=ipywidgets.Layout(min_width='50%'), style={'description_width': '150px'})\n",
163+
"model_entrypoint_widget = ipywidgets.Text(value='/', description='Model Entrypoint:', layout=ipywidgets.Layout(min_width='50%'), style={'description_width': '150px'})\n",
164+
"init_method_widget = ipywidgets.Text(value='', description='Initialization Method:', layout=ipywidgets.Layout(min_width='50%'), style={'description_width': '150px'})\n",
165+
"init_args_widget = ipywidgets.Text(value='', description='Initialization Arguments:', layout=ipywidgets.Layout(min_width='50%'), style={'description_width': '150px'})\n",
166+
"method_entrypoint_widget = ipywidgets.Text(value='', description='Method Entrypoint:', layout=ipywidgets.Layout(min_width='50%'), style={'description_width': '150px'})\n",
167+
"launch_button = ipywidgets.Button(description='Launch')\n",
168+
"launch_button.on_click(launch_button_click)\n",
169+
"launch_widgets = ipywidgets.VBox([model_uri_widget, model_entrypoint_widget, init_method_widget, method_entrypoint_widget, init_args_widget, launch_button])\n",
170+
"\n",
171+
"accordion = ipywidgets.Accordion(children=[connection_widgets, launch_widgets])\n",
172+
"accordion.set_title(0, 'Connection Parameters')\n",
173+
"accordion.set_title(1, 'Launch Configuration')\n",
174+
"\n",
175+
"output_widget = ipywidgets.Output()\n",
176+
"model_output_widget = ipywidgets.Output()\n",
177+
"simulation_output_widget = ipywidgets.Output()\n",
178+
"stack_output_widget = ipywidgets.Output()\n",
179+
"variable_output_widget = ipywidgets.Output()\n",
180+
"\n",
181+
"continue_button = ipywidgets.Button(description='▶', tooltip='Continue', layout=ipywidgets.Layout(width='50px'))\n",
182+
"continue_button.on_click(continue_button_click)\n",
183+
"next_button = ipywidgets.Button(description='➡', tooltip='Next', layout=ipywidgets.Layout(width='50px'))\n",
184+
"next_button.on_click(next_button_click)\n",
185+
"step_in_button = ipywidgets.Button(description='  ➡】', tooltip='Step In', layout=ipywidgets.Layout(width='50px'))\n",
186+
"step_in_button.on_click(step_in_button_click)\n",
187+
"step_out_button = ipywidgets.Button(description='【➡  ', tooltip='Step Out', layout=ipywidgets.Layout(width='50px'))\n",
188+
"step_out_button.on_click(step_out_button_click)\n",
189+
"terminate_button = ipywidgets.Button(description='⏹', tooltip='Terminate', layout=ipywidgets.Layout(width='50px'))\n",
190+
"terminate_button.on_click(terminate_button_click)\n",
191+
"restart_button = ipywidgets.Button(description='↻', tooltip='Restart', layout=ipywidgets.Layout(width='50px'))\n",
192+
"restart_button.on_click(restart_button_click)\n",
193+
"set_breakpoints_lines_widget = ipywidgets.Text(description='Breakpoints', placeholder='line1 line2 ...')\n",
194+
"set_breakpoints_button = ipywidgets.Button(description='Set', tooltip='Set Breakpoints', layout=ipywidgets.Layout(width='50px'))\n",
195+
"set_breakpoints_button.on_click(set_breakpoints_button_click)\n",
196+
"debug_widgets = ipywidgets.VBox([ipywidgets.HBox([continue_button, next_button, step_in_button, step_out_button, terminate_button, restart_button]),\n",
197+
" ipywidgets.HBox([set_breakpoints_lines_widget, set_breakpoints_button])])\n",
198+
"\n",
199+
"ipywidgets.VBox([accordion, output_widget, model_output_widget, simulation_output_widget, variable_output_widget, stack_output_widget, debug_widgets])"
200+
]
201+
}
202+
],
203+
"metadata": {
204+
"kernelspec": {
205+
"display_name": "Python 3",
206+
"language": "python",
207+
"name": "python3"
208+
},
209+
"language_info": {
210+
"codemirror_mode": {
211+
"name": "ipython",
212+
"version": 3
213+
},
214+
"file_extension": ".py",
215+
"mimetype": "text/x-python",
216+
"name": "python",
217+
"nbconvert_exporter": "python",
218+
"pygments_lexer": "ipython3",
219+
"version": "3.7.6"
220+
}
221+
},
222+
"nbformat": 4,
223+
"nbformat_minor": 4
224+
}

python_client/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
how to start jupyter client:
2+
3+
4+
### Install Miniconda
5+
6+
**This step is only necessary if you don't have conda installed**:
7+
- download the Miniconda installer for your OS [here](https://docs.conda.io/en/latest/miniconda.html)
8+
- run the installer following the instructions
9+
[here](https://conda.io/projects/conda/en/latest/user-guide/install/index.html#regular-installation)
10+
depending on your OS.
11+
12+
### Create conda environment
13+
14+
```sh
15+
# Create a conda environment with the required packages for this notebook:
16+
conda env create -f environment.yml
17+
# Activate your conda environment
18+
conda activate mep-client
19+
```

python_client/environment.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: mep-client
2+
channels:
3+
- conda-forge
4+
dependencies:
5+
- python=3.7
6+
- jupyter
7+
- jupyterlab
8+
- notebook
9+
- pip
10+
- ipywidgets>=7.5

0 commit comments

Comments
 (0)