Package: ipytangle
Reactive narratives inspired byTanglein theJupyter
Notebook.IPyTangle makes plain markdown into an interactive part of your
data-driven narrative.InstallationGet the most recent release with:pipinstallipytangleOr the bleeding-edge version:pipinstall-egit+https://github.com/bollwyvl/ipytangle#egg=IPyTangleUsageIn a Notebook, write this python…fromipytangleimporttangletangle(cookies=3,calories=lambdacookies:cookies*50)And this markdown:When you eat [`cookies`cookies](#:cookies), you consume [`calories`calories](#:calories).And you would see something like this:When you eat`2cookies <#:cookies>`__, you consume`150calories <#:>`__.And interacting with the links would cause the result to update.ExamplesAdapted from the originals fromTangle.CookiesCalifornia State Parks Proposition
21Additionally,ipytanglecomes with integration with IPython’sinteract, the easiest way to start using widgets.`Interacting with a
tangle <notebooks/examples/Interacting%20with%20a%20tangle.ipynb>`__Tangling up
``interact`<notebooks/examples/Tangling%20up%20interact.ipynb>`__Markdownipytangleimplements most of theTangleKitbaseline 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:[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
withipytangle, someformattinglibraries are
provided.windowglobals should also work :wink:.Only the generatedcodeblocks 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[<stringyexpression>](#:)just display a viewFor [`years`years](#:) have I trained Jedi.[<stringyexpression>](#:<variable>)display a view and update an integer based on dragging[made the kessel run in `distance` parsecs](#:distance)[<boolyexpression>](#:if)…[_ ``[<booly expression>](#elif)``_]…[_ ``[](#:else)`` _]…[](#:endif)mark some text (which may have other fields) to only display based on
conditionWhat's more foolish? The [`fool_is_more_foolish`](#:if)fool[](#:else)the fool who follows him(#:endif).you may also have anelseand any number ofelifs… because
they are markdown span-level elements, you may use newlines for easier
editing[`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.[](#:endif)Proposed Link Types[<stringyexpression>](#:<variable>:(int|float):<min>:<max>:<step>)display a view and update sometypeof variable. Additionally,minimummaximum andstep-size values can be specified.[](#:<variable>:inline)Replace thevariable, for which a linked widget has been registered
(TBD) with the widget itself inline. > Needs research.WidgetsAt it’s core,ipytangleprovidesTangle, an IPythonWidget.
Because of this, aTanglecan connect to any other IPython widgets
viatraitletsandlinks. Unlike many widgets, aTangledoesn’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 aTangle.ipytangle.tangleAs in the above example, this tries to be the most pythonic approach.fromipytangleimporttanglesquare=tangle(x=1,y=(1,lambdax:x*x))print("square is",square)square.x=2print("y is",square.y)square is <IPython.utils.traitlets.DynamicAutoTangle139717235746760 object at 0x7f1274347ef0>
y is 4Linking to other widgetstanglemakes working with other core and custom widgets easy,
automatically copying trait information. If a widget implementsvalue, as most of the core widgets do, you can reference it
directly. Alternately, specify atupleof(widget, "trait_name")to subscribe to that trait.fromIPython.html.widgetsimportIntSliderx=IntSlider(1)square=tangle(x=x,y=(1,lambdax:x*x))print("square is",square)x.value=2print("y is",square.y)square is <IPython.utils.traitlets.DynamicAutoTangle139717235839048 object at 0x7f1274362080>
y is 4ipytangle.TangleIf you are already familiar with widgets, subclassingTanglemight
be the most convenient:fromipytangleimportTanglefromIPython.utilsimporttraitletsclassSquare(Tangle):x=traitlets.CInt(1,sync=True)y=traitlets.CInt(1,sync=True)def_x_changed(self):self.y=self.x*self.xsquare=Square()print("square is",square)square.x=2print("y is",square.y)square is <__main__.Square object at 0x7f127434b7f0>
y is 4A future version of IPython Widgets will allow dynamically-added
traits with ``add_trait``. This will open up whole new areas for
tinkering. Stay tuned!FormattingIn addition to your locally-bound variables,ipytanglebundles
several nice JavaScript libraries and shortcuts for formatting text and
numbers: -`moment<http://momentjs.com/>`__ dates and times -`mathjs<http://mathjs.org/>`__ scientific
units- [numeral`](http://numeraljs.com/) currency, and
miscellanyInspirationOf course, Brett Victor’sTangleis
the primary inspiration, as well as: -tributary-derby-d3RoadmapsupportTangleKitbaselinefloatswitch\(L_AT^EX\)(sic)sparklines, distributions, etc.offline/nbviewer?