Skip to content

Commit bc5ec8b

Browse files
committed
create an idom.run util
1 parent 35dcf47 commit bc5ec8b

25 files changed

+282
-341
lines changed

docs/main.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from sanic import Sanic
77
from sanic import response
88

9+
import idom
910
from idom.widgets.utils import multiview
1011
from idom.client.manage import APP_DIR
1112
from idom.server.sanic import PerClientStateServer
@@ -27,22 +28,30 @@ async def forward_to_index(request):
2728
examples_dir = here / "source" / "examples"
2829
sys.path.insert(0, str(examples_dir))
2930

30-
for file in examples_dir.iterdir():
31-
if not file.is_file() or not file.suffix == ".py" or file.stem.startswith("_"):
32-
continue
33-
34-
with file.open() as f:
35-
try:
36-
exec(
37-
f.read(),
38-
{
39-
"display": mount[file.stem],
40-
"__file__": str(file),
41-
"__name__": f"widgets.{file.stem}",
42-
},
43-
)
44-
except Exception as error:
45-
raise RuntimeError(f"Failed to execute {file}") from error
31+
original_run = idom.run
32+
33+
try:
34+
for file in examples_dir.iterdir():
35+
if not file.is_file() or not file.suffix == ".py" or file.stem.startswith("_"):
36+
continue
37+
38+
# Modify the run function so when we exec the file
39+
# instead of running a server we mount the view.
40+
idom.run = mount[file.stem]
41+
42+
with file.open() as f:
43+
try:
44+
exec(
45+
f.read(),
46+
{
47+
"__file__": str(file),
48+
"__name__": f"widgets.{file.stem}",
49+
},
50+
)
51+
except Exception as error:
52+
raise RuntimeError(f"Failed to execute {file}") from error
53+
except Exception:
54+
idom.run = original_run
4655

4756
server = (
4857
PerClientStateServer(element)

docs/source/examples.rst

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
Examples
22
========
33

4-
You can find the following examples and more on binder |launch-binder|:
4+
You can also try these examples out on binder |launch-binder|:
55

66
.. contents::
77
:local:
88
:depth: 1
99

1010

11-
Display Function
12-
----------------
11+
Displaying These Examples
12+
-------------------------
1313

1414
Depending on how you plan to use these examples you'll need different
1515
boilerplate code.
@@ -18,12 +18,6 @@ In all cases we define a ``display(element)`` function which will display the
1818
view. In a Jupyter Notebook it will appear in an output cell. If you're running
1919
``idom`` as a webserver it will appear at http://localhost:8765/client/index.html.
2020

21-
.. note::
22-
23-
The :ref:`Shared Client Views` example requires ``SharedClientStateServer`` server instead
24-
of the ``PerClientStateServer`` server shown in the boilerplate below. Be sure to wwap it
25-
out when you get there.
26-
2721

2822
**Local Python File**
2923

@@ -138,36 +132,6 @@ Click the bars to trigger an event 👇
138132
.. example:: custom_chart
139133

140134

141-
Shared Client Views
142-
-------------------
143-
144-
This example requires the :class:`~idom.server.sanic.SharedClientStateServer`. Be sure
145-
to replace it in your boilerplate code before going further! Once you've done this we
146-
can just re-display our :ref:`Slideshow` example using the new server. Now all we need
147-
to do is connect to the server with a couple clients to see that their views are synced.
148-
This can be done by navigating to the server URL in seperate browser tabs. Likewise if
149-
you're using a Jupyter Notebook you would display it in multiple cells like this:
150-
151-
**Jupyter Notebook**
152-
153-
.. code-block::
154-
155-
# Cell 1
156-
... # boiler plate with SharedClientState server
157-
158-
# Cell 2
159-
... # code from the Slideshow example
160-
161-
# Cell 3
162-
widget = display(Slideshow)
163-
164-
# Cell 4
165-
widget # this is our first view
166-
167-
# Cell 5
168-
widget # this is out second view
169-
170-
171135
Material UI Slider
172136
------------------
173137

@@ -178,16 +142,6 @@ Move the slider and see the event information update 👇
178142
.. example:: material_ui_slider
179143

180144

181-
Semantic UI Buttons
182-
-------------------
183-
184-
Assuming you already installed ``semantic-ui-react`` as in the :ref:`Install Javascript Modules` section:
185-
186-
Click the buttons and see the event information update 👇
187-
188-
.. example:: primary_secondary_buttons
189-
190-
191145
.. Links
192146
.. =====
193147

docs/source/examples/click_count.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ def ClickCount():
1111
)
1212

1313

14-
display(ClickCount)
14+
idom.run(ClickCount)

docs/source/examples/custom_chart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ def ShowChartClicks():
3737
)
3838

3939

40-
display(ShowChartClicks)
40+
idom.run(ShowChartClicks)

docs/source/examples/material_ui_button_no_action.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
material_ui = idom.Module("@material-ui/core")
44
MaterialButton = material_ui.Import("Button", fallback="loading...")
55

6-
display(MaterialButton, {"color": "primary", "variant": "contained"}, "Hello World!")
6+
7+
@idom.element
8+
def ViewMaterialButton():
9+
return MaterialButton({"color": "primary", "variant": "contained"}, "Hello World!")
10+
11+
12+
idom.run(ViewMaterialButton)

docs/source/examples/material_ui_button_on_click.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ def ViewSliderEvents():
2424
)
2525

2626

27-
display(ViewSliderEvents)
27+
idom.run(ViewSliderEvents)

docs/source/examples/material_ui_slider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ def ViewSliderEvents():
2727
)
2828

2929

30-
display(ViewSliderEvents)
30+
idom.run(ViewSliderEvents)

docs/source/examples/matplotlib_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ def linspace(start, stop, n):
8787
yield start + h * i
8888

8989

90-
display(PolynomialPlot)
90+
idom.run(PolynomialPlot)

docs/source/examples/primary_secondary_buttons.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

docs/source/examples/simple_dashboard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ async def interval() -> None:
8585
return asyncio.ensure_future(interval())
8686

8787

88-
display(RandomWalk)
88+
idom.run(RandomWalk)

0 commit comments

Comments
 (0)