Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Tangle reactive documents in the IPython Notebook

Project description


|build-badge| |pypi-badge|

Reactive narratives inspired by
`Tangle <>`__ in the `Jupyter
Notebook <>`__.

IPyTangle makes plain markdown into an interactive part of your
data-driven narrative.

.. |build-badge| image::
.. |pypi-badge| image::

.. figure:: screenshots/cookies/02.png


Get the most recent release with:

.. code:: bash

pip install ipytangle

Or the bleeding-edge version:

.. code:: bash

pip install -e git+


In a Notebook, write this python...

.. code:: python

from ipytangle import tangle

tangle(cookies=3, calories=lambda cookies: cookies * 50)

And this markdown:

.. code:: markdown

When you eat [`cookies` cookies](#:cookies), you consume [`calories` calories](#:calories).

And you would see something like this:

When you eat ```2`` cookies <#:cookies>`__, you consume ```150``
calories <#:>`__.

And interacting with the links would cause the result to update.


Adapted from the originals from
`Tangle <>`__.

- `Cookies <notebooks/examples/Cookies.ipynb>`__
- `California State Parks Proposition
21 <notebooks/examples/Proposition%2021.ipynb>`__


``ipytangle`` implements most of the
`TangleKit <>`__
baseline as markdown links. Because it adds no new notation, unrendered
Tangle Markdown should still render in a useful way.

Generally, think of a link as:

.. code:: markdown

[what the user sees](#:tangle types)

In the link text, backticks, **\`\`** are used to represent a JavaScript
expression that will be updated on user interaction, cascading updates,
or other updates from the kernel. In addition to any variables defined
with ``ipytangle``, some `formatting <#Formatting>`__ libraries are
provided. ``window`` globals should also work :wink:.

Only the generated ``code`` blocks will be transformed, the rest of the
elements (if any) will be preserved. This means you can use images,
inside the label of a control, for example.

Link Types

``[<stringy expression>](#:)``

just display a view

.. code:: markdown

For [`years` years](#:) have I trained Jedi.

``[<stringy expression>](#:<variable>)``

display a view and update an integer based on dragging

.. code:: markdown

[made the kessel run in `distance` parsecs](#:distance)

``[<booly expression>](#:if)`` ... *[\_ ``[<booly expression>](#elif)``\ \_]* ... *[\_ ``[](#:else)`` \_]* ... ``[](#:endif)``

mark some text (which may have other fields) to only display based on

.. code:: markdown

What's more foolish? The [`fool_is_more_foolish`](#:if)fool[](#:else)the fool who follows him(#:endif).

you may also have an ``else`` and any number of ``elif``\ s... because
they are markdown span-level elements, you may use newlines for easier

.. code:: markdown

[`feeling == "bad"`](#:if) I have a bad feeling about this.
[`feeling == "cautious"`](#:elif) You will never find a more wretched hive of scum and villainy.
[](#:else) Search your feelings.

Proposed Link Types

``[<stringy expression>](#:<variable>:(int|float):<min>:<max>:<step>)``

display a view and update some ``type`` of variable. Additionally,
``min``\ imum ``max``\ imum and ``step``-size values can be specified.


Replace the ``variable``, for which a linked widget has been registered
(TBD) with the widget itself inline. > Needs research.


At it's core, ``ipytangle`` provides ``Tangle``, an IPython ``Widget``.
Because of this, a ``Tangle`` can connect to any other IPython widgets
via *traitlets* and ``links``. Unlike many widgets, a ``Tangle`` doesn't
do much with its screen real estate, and might not be very interesting
to put inside a layout. Yet.

There are several methods for creating a ``Tangle``.


As in the above example, this tries to be the most pythonic approach.

.. code:: python

from ipytangle import tangle

square = tangle(x=1, y=(1, lambda x: x*x))

print("square is", square)
square.x = 2
print("y is", square.y)

.. parsed-literal::

square is <IPython.utils.traitlets.DynamicAutoTangle139717235746760 object at 0x7f1274347ef0>
y is 4

Linking to other widgets

``tangle`` makes working with other core and custom widgets easy,
automatically copying trait information. If a widget implements
``value``, as most of the core widgets do, you can reference it
directly. Alternately, specify a ``tuple`` of ``(widget, "trait_name")``
to subscribe to that trait.

.. code:: python

from IPython.html.widgets import IntSlider

x = IntSlider(1)
square = tangle(x=x, y=(1, lambda x: x*x))

print("square is", square)
x.value = 2
print("y is", square.y)

.. parsed-literal::

square is <IPython.utils.traitlets.DynamicAutoTangle139717235839048 object at 0x7f1274362080>
y is 4


If you are already familiar with widgets, subclassing ``Tangle`` might
be the most convenient:

.. code:: python

from ipytangle import Tangle
from IPython.utils import traitlets

class Square(Tangle):
x = traitlets.CInt(1, sync=True)
y = traitlets.CInt(1, sync=True)

def _x_changed(self):
self.y = self.x * self.x

square = Square()

print("square is", square)
square.x = 2
print("y is", square.y)

.. parsed-literal::

square is <__main__.Square object at 0x7f127434b7f0>
y is 4

*A future version of IPython Widgets will allow dynamically-added
traits with ``add_trait``. This will open up whole new areas for
tinkering. Stay tuned!*


In addition to your locally-bound variables, ``ipytangle`` bundles
several nice JavaScript libraries and shortcuts for formatting text and
numbers: - ```moment`` <>`__ dates and times -
```mathjs`` <>`__ scientific
units\ ``- [``\ numeral\`]( currency, and


Of course, Brett Victor's `Tangle <>`__ is
the primary inspiration, as well as: -
`tributary <>`__ - `derby <>`__ -
`d3 <>`__


- support
`TangleKit <>`__
- float
- switch
- :math:`L_AT^EX` (sic)
- sparklines, distributions, etc.
- offline/nbviewer?

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for ipytangle, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size ipytangle-0.1.1-py2.py3-none-any.whl (1.2 MB) File type Wheel Python version 3.4 Upload date Hashes View
Filename, size ipytangle-0.1.1.tar.gz (525.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page