Skip to content

Commit 2ccbadb

Browse files
committed
fix docs with jupyterlab
1 parent e8fa6de commit 2ccbadb

11 files changed

Lines changed: 100 additions & 847 deletions

File tree

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ A few reasons why you may consider using it:
5555
Getting started
5656
===============
5757

58+
5859
To install Python State Machine, run this command in your terminal:
5960

6061
.. code-block:: console

docs/auto_examples/guess_the_number_machine.rst

Lines changed: 16 additions & 450 deletions
Large diffs are not rendered by default.

docs/auto_examples/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ Examples
66
Below is a gallery of StateMachine examples.
77

88

9+
.. tip:: Try Python State Machine directly from your browser!
10+
11+
Run our examples and try hacks right now on this `JupyterLab <../lite/lab/index.html>`_
12+
13+
Powered by the amazing https://pyodide.org/.
14+
15+
916

1017
.. raw:: html
1118

docs/auto_examples/order_control_rich_model_machine.rst

Lines changed: 28 additions & 388 deletions
Large diffs are not rendered by default.

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,5 +359,6 @@
359359
"first_notebook_cell": (
360360
"# import piplite\n"
361361
"# await piplite.install('python-statemachine[diagrams]')\n"
362+
"# import patch_repr_svg\n"
362363
),
363364
}

docs/examples/patch_repr_svg.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
3+
4+
def patch__repr_svg_(): # pragma: no cover
5+
"""
6+
You're running this example directly from your browser! By using the amazing
7+
https://pyodide.org/.
8+
9+
Since we're in a browser, the default dependency of GraphViz installed locally don't work. So
10+
this cell is an option to see our examples and try the library on the fly without the need to
11+
install anything.
12+
13+
We've to patch `StateMachine._repr_svg_` to retrieve the svg diagram online as the graphviz is
14+
not available.
15+
16+
This method will serialize and request the graph to an external service: https://quickchart.io.
17+
By using de diagrams support you trust quickchart.io. Please read
18+
https://quickchart.io/documentation/faq/.
19+
20+
See also https://python-statemachine.readthedocs.io/en/latest/diagram.html
21+
"""
22+
from urllib.parse import quote
23+
from statemachine import StateMachine
24+
25+
def show_sm(sm):
26+
url = "https://quickchart.io/graphviz?graph={}".format(
27+
quote(sm._graph().to_string())
28+
)
29+
return '<svg width="auto" height="auto"><image xlink:href="{}"/>'.format(url)
30+
31+
StateMachine._repr_svg_ = show_sm
32+
33+
34+
if sys.platform == "emscripten": # pragma: no cover
35+
# https://pyodide.org/ is the runtime!
36+
patch__repr_svg_()
37+
print(patch__repr_svg_.__doc__)
38+
print("'StateMachine._repr_svg_' patched!")

docs/releases/1.0.0.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# StateMachine 1.0.0
22

3-
*January 11, 2022*
3+
*January 11, 2023*
44

55
This release tag was replaced by [](1.0.1.md) due to an error on the metadata when uploading to
66
pypi.

docs/releases/1.0.1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# StateMachine 1.0.1
22

3-
*January 11, 2022*
3+
*January 11, 2023*
44

55
Welcome to StateMachine 1.0.1!
66

statemachine/statemachine.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,6 @@ def add_observer(self, *observers):
105105
self._visit_states_and_transitions(lambda x: x._add_observer(*resolvers))
106106
return self
107107

108-
def _repr_html_(self):
109-
return '<div class="statemachine">{}</div>'.format(self._repr_svg_())
110-
111108
def _repr_svg_(self):
112109
return self._graph().create_svg().decode()
113110

tests/examples/README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@ Examples
22
--------
33

44
Below is a gallery of StateMachine examples.
5+
6+
7+
.. tip:: Try Python State Machine directly from your browser!
8+
9+
Run our examples and try hacks right now on this `JupyterLab <../lite/lab/index.html>`_
10+
11+
Powered by the amazing https://pyodide.org/.

0 commit comments

Comments
 (0)