Skip to content

Commit bdbde83

Browse files
committed
fix doc examples following changes
1 parent 5ea08b7 commit bdbde83

16 files changed

+55
-126
lines changed

Diff for: docs/main.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ async def forward_to_index(request):
2828
examples_dir = here / "source" / "examples"
2929
sys.path.insert(0, str(examples_dir))
3030

31-
original_run = idom.run
3231

32+
original_run = idom.run
3333
try:
3434
for file in examples_dir.iterdir():
3535
if not file.is_file() or not file.suffix == ".py" or file.stem.startswith("_"):
@@ -50,14 +50,11 @@ async def forward_to_index(request):
5050
)
5151
except Exception as error:
5252
raise RuntimeError(f"Failed to execute {file}") from error
53-
except Exception:
53+
finally:
5454
idom.run = original_run
5555

56-
server = (
57-
PerClientStateServer(element)
58-
.configure({"redirect_root_to_index": False})
59-
.register(app)
60-
)
56+
57+
server = PerClientStateServer(element, {"redirect_root_to_index": False}).register(app)
6158

6259

6360
def prod():
@@ -73,6 +70,7 @@ def local(path=""):
7370
import webbrowser
7471

7572
thread = server.daemon("127.0.0.1", 5000)
73+
path = f"docs/{path}" if path else ""
7674
webbrowser.open(f"http://127.0.0.1:5000/{path}")
7775
thread.join()
7876

Diff for: docs/source/examples.rst

-52
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,6 @@ You can also try these examples out on binder |launch-binder|:
88
:depth: 1
99

1010

11-
Displaying These Examples
12-
-------------------------
13-
14-
Depending on how you plan to use these examples you'll need different
15-
boilerplate code.
16-
17-
In all cases we define a ``display(element)`` function which will display the
18-
view. In a Jupyter Notebook it will appear in an output cell. If you're running
19-
``idom`` as a webserver it will appear at http://localhost:8765/client/index.html.
20-
21-
22-
**Local Python File**
23-
24-
.. code-block::
25-
26-
import idom
27-
from idom.server.sanic import PerClientStateServer
28-
29-
def display(element, *args, **kwargs):
30-
PerClientStateServer(element, *args, **kwargs).run("127.0.0.1", 8765)
31-
32-
@idom.element
33-
def Main(self):
34-
# define your element here
35-
...
36-
37-
if __name__ == "__main__":
38-
display(Main)
39-
40-
41-
**Jupyter Notebook**
42-
43-
.. code-block::
44-
45-
from idom.widgets.jupyter import init_display
46-
display = init_display("127.0.0.1")
47-
48-
@idom.element
49-
def MyElement():
50-
# define your element here
51-
...
52-
53-
jupyter_widget = display(MyElement)
54-
55-
.. note::
56-
57-
The ``init_display`` function checks environment variables to try and infer whether
58-
it's in a Jupyterhub instance (e.g. mybinder.org) and if so, assumes the presence of a
59-
`jupyter_server_proxy <https://github.com/jupyterhub/jupyter-server-proxy>`_. If this
60-
doesn't work please `post an issue <https://github.com/rmorshea/idom/issues>`_.
61-
62-
6311
Slideshow
6412
---------
6513

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

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

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

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class GameState(enum.Enum):
1414

1515

1616
@idom.element
17-
def GameView(grid_size, block_scale):
17+
def GameView():
1818
game_state, set_game_state = idom.hooks.use_state(GameState.init)
1919

2020
if game_state == GameState.play:
21-
return GameLoop(grid_size, block_scale, set_game_state)
21+
return GameLoop(grid_size=6, block_scale=50, set_game_state=set_game_state)
2222

2323
start_button = idom.html.button(
2424
{"onClick": lambda event: set_game_state(GameState.play)},
@@ -167,4 +167,4 @@ def assign_grid_block_color(grid, point, color):
167167
block["attributes"]["style"]["backgroundColor"] = color
168168

169169

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

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def decrement(last_count):
1010

1111

1212
@idom.element
13-
def Counter(initial_count):
13+
def Counter():
14+
initial_count = 0
1415
count, set_count = idom.hooks.use_state(initial_count)
1516
return idom.html.div(
1617
f"Count: {count}",
@@ -20,4 +21,4 @@ def Counter(initial_count):
2021
)
2122

2223

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

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

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

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

7-
idom.run(VictoryBar, {"style": {"parent": {"width": "500px"}}})
7+
idom.run(
8+
idom.element(
9+
lambda: VictoryBar({"style": {"parent": {"width": "500px"}}}),
10+
)
11+
)

Diff for: docs/source/index.rst

+14-15
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,11 @@ Libraries for defining and controlling interactive webpages with Python
2121
api
2222

2323

24-
Try it Now!
25-
-----------
26-
27-
- Using working :ref:`Examples`
28-
29-
- In a Jupyter Notebook - |launch-binder|
30-
31-
3224
Early Days
3325
----------
3426

35-
IDOM is still young. If you have ideas or find a bug, be sure to post an
36-
`issue`_ or create a `pull request`_. Thanks in advance!
27+
IDOM is still young. Be sure to post any `issues`_ you have or contribute by creating a
28+
`pull request`_.
3729

3830

3931
At a Glance
@@ -44,25 +36,32 @@ user clicks an image:
4436

4537
.. example:: slideshow
4638

47-
You can try out a **Live Example** by selecting the tab and enabling the widget.
39+
Try selecting the "Live Example" tab in the view above and enabling the widget.
4840

4941
Once activated try clicking the displayed image to make it change 🖱️
5042

5143
.. note::
5244

53-
You can display the same thing in a Jupyter Notebook using widgets!
45+
You can display the same thing in a Jupyter Notebook by installing ``idom-jupyter``:
46+
47+
.. code-block:: bash
48+
49+
pip install idom-jupyter
50+
51+
Then simply
5452

5553
.. code-block::
5654
57-
idom.JupyterDisplay(f"https://{host}:{port}")
55+
import idom_jupyter
56+
idom_jupyter.display(Slideshow)
5857
59-
For info on working with IDOM in Jupyter see some :ref:`examples <Display Function>`.
58+
Try it out on Binder now: |launch-binder|
6059

6160

6261
.. Links
6362
.. =====
6463
65-
.. _issue: https://github.com/rmorshea/idom/issues
64+
.. _issues: https://github.com/rmorshea/idom/issues
6665
.. _pull request: https://github.com/rmorshea/idom/pulls
6766
.. _IDOM Sandbox: https://idom-sandbox.herokuapp.com
6867
.. |launch-binder| image:: https://mybinder.org/badge_logo.svg

Diff for: docs/source/javascript-modules.rst

+4-22
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,12 @@ packages.
3131
Installing React Components
3232
---------------------------
3333

34-
Before you start:
35-
36-
- Be sure that you've installed `npm <https://www.npmjs.com/get-npm>`__.
34+
.. note::
3735

38-
- We're assuming the presence of a :ref:`Display Function` for our examples.
36+
Be sure that you've installed `npm <https://www.npmjs.com/get-npm>`__.
3937

40-
Once you've done this you can get started right away. In this example we'll be using the
41-
ubiquitous React-based UI framework `Material UI`_ which can be installed using the
42-
``idom`` CLI:
38+
In this example we'll be using the ubiquitous React-based UI framework `Material UI`_
39+
which can be easily installed using the ``idom`` CLI:
4340

4441
.. code-block:: bash
4542
@@ -58,21 +55,6 @@ Once the package has been succesfully installed, you can import and display the
5855
.. example:: material_ui_button_no_action
5956

6057

61-
.. note::
62-
63-
Styling for Material UI is already built into this page so in the following examples
64-
we've omitted a link to the style sheet which would otherwise be created with:
65-
66-
.. code-block::
67-
68-
material_ui_style = idom.html.link(
69-
{
70-
"rel": "stylesheet",
71-
"href": "https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap",
72-
}
73-
)
74-
75-
7658
Passing Props To Components
7759
---------------------------
7860

Diff for: idom/core/element.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ def __repr__(self) -> str:
7979
args = sig.bind(*self._args, **self._kwargs).arguments
8080
items = ", ".join(f"{k}={v!r}" for k, v in args.items())
8181
if items:
82-
return f"{self._function.__name__}({id(self)}, {items})"
82+
return f"{self._function.__name__}({hex(id(self))}, {items})"
8383
else:
84-
return f"{self._function.__name__}({id(self)})"
84+
return f"{self._function.__name__}({hex(id(self))})"

Diff for: idom/widgets/utils.py

+3
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,6 @@ def __call__(self, constructor: ElementConstructor) -> str:
160160
view_id = str(self._next_auto_id)
161161
self._views[view_id] = constructor
162162
return view_id
163+
164+
def __repr__(self):
165+
return f"{type(self).__name__}({self._views})"

Diff for: scripts/all_examples.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ def main():
2626
if not example_file.stem.startswith("_"):
2727
with example_file.open() as f_obj:
2828
try:
29+
idom.run = lambda func: views.append((example_file.stem, func))
2930
exec(
3031
f_obj.read(),
3132
{
32-
"display": lambda f, *a, **kw: views.append(
33-
(example_file.stem, f, a, kw)
34-
),
3533
"__file__": str(file),
3634
"__name__": f"widgets.{file.stem}",
3735
},
@@ -44,9 +42,9 @@ def main():
4442
@idom.element
4543
def AllExamples():
4644
examples = []
47-
for title, f, a, kw in views:
45+
for title, func in views:
4846
examples.append(idom.html.h1(title))
49-
examples.append(f(*a, **kw))
47+
examples.append(func())
5048
examples.append(idom.html.hr({"style": {"margin-top": "20px"}}))
5149
return idom.html.div({"style": {"margin": "20px"}}, examples)
5250

Diff for: scripts/local-docs.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ set -e
22
python scripts/install_doc_js_modules.py
33
sphinx-build -E -b html docs/source docs/build
44
cd docs
5-
python -c "import main; main.local('docs/$1')"
5+
python -c "import main; main.local('$1')"
66
cd ../

Diff for: scripts/one_example.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import sys
22
from pathlib import Path
33

4-
from idom.widgets.utils import hotswap
5-
from idom.server.sanic import PerClientStateServer
4+
import idom
65

76
from scripts.install_doc_js_modules import install_doc_js_modules
87

@@ -33,21 +32,18 @@ def main():
3332
_print_available_options()
3433
return
3534

36-
mount, element = hotswap()
37-
server = PerClientStateServer(element)
35+
idom_run = idom.run
36+
idom.run = lambda element: idom_run(element, port=5000)
3837

3938
with example_file.open() as f:
4039
exec(
4140
f.read(),
4241
{
43-
"display": mount,
4442
"__file__": str(file),
4543
"__name__": f"widgets.{file.stem}",
4644
},
4745
)
4846

49-
server.run("127.0.0.1", 5000)
50-
5147

5248
def _print_available_options():
5349
for found_example_file in examples_dir.glob("*.py"):

Diff for: sys

Whitespace-only changes.

Diff for: tests/test_core/test_element.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def MyElement(a, *b, **c):
88

99
m_e = MyElement(1, 2, 3, x=4, y=5)
1010

11-
expected = f"MyElement({id(m_e)}, a=1, b=(2, 3), c={{'x': 4, 'y': 5}})"
11+
expected = f"MyElement({hex(id(m_e))}, a=1, b=(2, 3), c={{'x': 4, 'y': 5}})"
1212
assert repr(m_e) == expected
1313

1414

Diff for: tests/test_core/test_layout.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def MyElement():
2323

2424
my_element = MyElement()
2525
layout = idom.Layout(my_element)
26-
assert str(layout) == f"Layout(MyElement({id(my_element)}))"
26+
assert str(layout) == f"Layout(MyElement({hex(id(my_element))}))"
2727

2828

2929
def test_layout_expects_abstract_element():

0 commit comments

Comments
 (0)