|
| 1 | +Javascript Modules |
| 2 | +================== |
| 3 | + |
| 4 | +.. note:: |
| 5 | + |
| 6 | + This is recent feature of IDOM. If you have a problem following this tutorial |
| 7 | + `post an issue <https://github.com/rmorshea/idom/issues>`__. |
| 8 | + |
| 9 | +While IDOM is a great tool for displaying HTML and respond to browser events with |
| 10 | +pure Python, there are other projects which already allow you to do this inside |
| 11 | +`Jupyter Notebooks <https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Basics.html>`__ |
| 12 | +or in |
| 13 | +`webpages <https://blog.jupyter.org/and-voil%C3%A0-f6a2c08a4a93?gi=54b835a2fcce>`__. |
| 14 | +The real power of IDOM comes from its ability to seemlessly leverage the existing |
| 15 | +ecosystem of `React components <https://reactjs.org/docs/components-and-props.html>`__. |
| 16 | +So long as you can install a React library using `Snowpack <https://www.snowpack.dev/>`__ |
| 17 | +you can use it in your IDOM layout. You can even define your own Javascript modules |
| 18 | +which use these third party Javascript packages. |
| 19 | + |
| 20 | + |
| 21 | +Installing React Components |
| 22 | +--------------------------- |
| 23 | + |
| 24 | +.. note:: |
| 25 | + |
| 26 | + - Be sure that you've installed `npm <https://www.npmjs.com/get-npm>`__. |
| 27 | + |
| 28 | + - We're assuming the presence of a :ref:`Display Function` for our examples. |
| 29 | + |
| 30 | +Once you've done this you can get started right away. In this example we'll be using a |
| 31 | +charting library for React called `Victory <https://formidable.com/open-source/victory/>`__. |
| 32 | +Installing it in IDOM is quite simple: |
| 33 | + |
| 34 | +.. code-block:: |
| 35 | +
|
| 36 | + import idom |
| 37 | + # this may take a minute to download and install |
| 38 | + victory = idom.Module(name="victory", install=True) |
| 39 | +
|
| 40 | +You can install a specific version using `` install="[email protected]`` or any other |
| 41 | +standard javascript dependency specifier. Alternatively, if you need to access a module |
| 42 | +in a subfolder of your desired Javascript package, you can provide ``name="path/to/module"`` |
| 43 | +and ``install"my-package"``. |
| 44 | + |
| 45 | +With that out of the way can import a component from Victory: |
| 46 | + |
| 47 | +.. code-block:: |
| 48 | +
|
| 49 | + VictoryBar = victory.Import("VictoryBar") |
| 50 | +
|
| 51 | +Using the ``VictoryBar`` chart component is as simple as displaying it: |
| 52 | + |
| 53 | +.. code-block:: |
| 54 | +
|
| 55 | + display(VictoryBar) |
| 56 | +
|
| 57 | +
|
| 58 | +Passing Props To Components |
| 59 | +--------------------------- |
| 60 | + |
| 61 | +Under construction... |
| 62 | + |
| 63 | + |
| 64 | +Defining Your Own Modules |
| 65 | +------------------------- |
0 commit comments