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

Diff for: docs/main.py

+25-16
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)

Diff for: docs/source/examples.rst

+3-49
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

Diff for: docs/source/examples/click_count.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ def ClickCount():
1111
)
1212

1313

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

Diff for: docs/source/examples/custom_chart.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ def ShowChartClicks():
3737
)
3838

3939

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

Diff for: docs/source/examples/material_ui_button_no_action.py

+7-1
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)

Diff for: docs/source/examples/material_ui_button_on_click.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ def ViewSliderEvents():
2424
)
2525

2626

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

Diff for: docs/source/examples/material_ui_slider.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ def ViewSliderEvents():
2727
)
2828

2929

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

Diff for: docs/source/examples/matplotlib_plot.py

+1-1
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)

Diff for: docs/source/examples/primary_secondary_buttons.py

-42
This file was deleted.

Diff for: docs/source/examples/simple_dashboard.py

+1-1
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)

Diff for: docs/source/examples/slideshow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ def next_image(event):
1717
)
1818

1919

20-
display(Slideshow)
20+
idom.run(Slideshow)

Diff for: docs/source/examples/snake_game.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,4 @@ def assign_grid_block_color(grid, point, color):
167167
block["attributes"]["style"]["backgroundColor"] = color
168168

169169

170-
display(GameView, 6, 50)
170+
idom.run(GameView, 6, 50)

Diff for: docs/source/examples/todo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ async def remove_task(event, index=index):
2626
return idom.html.div(task_input, task_table)
2727

2828

29-
display(Todo)
29+
idom.run(Todo)

Diff for: docs/source/examples/use_reducer_counter.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def reducer(count, action):
1313

1414

1515
@idom.element
16-
def Counter(initial_count):
17-
count, dispatch = idom.hooks.use_reducer(reducer, initial_count)
16+
def Counter():
17+
count, dispatch = idom.hooks.use_reducer(reducer, 0)
1818
return idom.html.div(
1919
f"Count: {count}",
2020
idom.html.button({"onClick": lambda event: dispatch("reset")}, "Reset"),
@@ -23,4 +23,4 @@ def Counter(initial_count):
2323
)
2424

2525

26-
display(Counter, 0)
26+
idom.run(Counter)

Diff for: docs/source/examples/use_state_counter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ def Counter(initial_count):
2020
)
2121

2222

23-
display(Counter, 0)
23+
idom.run(Counter, 0)

Diff for: docs/source/examples/victory_chart.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
VictoryBar = victory.Import("VictoryBar", fallback="loading...")
66

7-
display(VictoryBar, {"style": {"parent": {"width": "500px"}}})
7+
idom.run(VictoryBar, {"style": {"parent": {"width": "500px"}}})

Diff for: docs/source/exts/interactive_widget.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ def run(self):
3636
}}
3737
.enable-widget-button {{
3838
padding: 10px;
39+
color: #ffffff !important;
40+
text-transform: uppercase;
41+
text-decoration: none;
42+
background: #3980b9;
43+
border: 2px solid #3980b9 !important;
44+
transition: all 0.2s ease 0s;
45+
box-shadow: 0 5px 10px grey;
46+
}}
47+
.enable-widget-button:hover {{
48+
color: #3980b9 !important;
49+
background: #ffffff;
50+
transition: all 0.2s ease 0s;
51+
}}
52+
.enable-widget-button:focus {{
53+
outline: 0 !important;
54+
transform: scale(0.98);
55+
transition: all 0.2s ease 0s;
3956
}}
4057
</style>
4158
<div>
@@ -48,7 +65,7 @@ def run(self):
4865
4966
const mount = document.getElementById("{container_id}");
5067
const enableWidgetButton = document.createElement("button");
51-
enableWidgetButton.innerHTML = "Enable Widget";
68+
enableWidgetButton.innerHTML = "Enable Widget";
5269
enableWidgetButton.setAttribute("class", "enable-widget-button")
5370
5471
enableWidgetButton.addEventListener("click", () => {{

0 commit comments

Comments
 (0)