JuliaGizmos / Interact.jl

Interactive widgets to play with your Julia code
Other
525 stars 76 forks source link

Interact

Build Status

Play with Julia code and share the fun!

This package is a collection of web-based widgets that you can use to:

It works in Jupyter notebooks, Atom IDE, or as a plain old web page.

Usage

@manipulate

The simplest way to use Interact is via the @manipulate macro.

interacting with sliders and toggles

The syntax of @manipulate is

@manipulate for var1=<object>, var2=<object>, etc...
    # do something with a, b, etc...
end

Here <object> can be one of the following:

Widgets à la carte

You can also create widgets by themselves using the same input scheme. Just pass the <object> to widget() which has the syntax:

widget(<object>; label="<my label>")

For example,

px=widget(0:0.01:.3, label="px")
py=widget(0:0.01:.3, label="py")
plane = widget(Dict("x=0" => 1, "y=0" => 2), label="Section plane");

Then you can use map over the widgets to create new interactive output that depends on these widgets!

interactive_plot = map(plotsos, px, py, plane)

And put them all together into a nice little layout:

# stack vertically
vbox(
    hbox(px, py, plane), # stack horizontally
    interactive_plot)

If you would like even more customization for your widgets, each type of widget has a different set of options you can tweak and different ways of updating it. So head over to the documentation about them!

This demo has color and date pickers!

Getting started

Interact's output can be displayed either within Jupyter/Jupyterlab notebooks, the Atom text editor, or as a stand-alone web page. Below are the set up instructions for each of these front ends:

IJulia + Jupyter notebooks

To set up Interact to work in Jupyter notebooks, first install Interact, IJulia and WebIO packages. Then install the required Jupyter extension by running:

using WebIO
WebIO.install_jupyter_nbextension()

Important: If you performed this step while the Jupyter notebook server is running, you will need to restart it for the extension to take effect.

If you have multiple Jupyter installations: The WebIO.install_jupyter_nbextension([jupyter]) function takes the path to the jupyter binary as an optional argument. If omitted, the installation first tries to find jupyter in your OS's default path. If it does not exist there, it tries to find a version that was installed using Conda.jl (which is the default when IJulia is installed without having an existing Jupyter set up, or by forcing it to install a copy via Conda.) If you have a jupyter binary in your OS path but also have a version installed via Conda and want to use the one installed via Conda, then you can set this keyword argument to be WebIO.find_jupyter_cmd(condajl=true). For more information about the Jupyter integration, see WebIO's documentation. There is more troubleshooting information here.

IJulia + JupyterLab

To set up Interact to work in JupyterLab, first install Interact, IJulia and WebIO packages. Then install the required JupyterLab extension by running:

using WebIO
WebIO.install_jupyter_labextension()

Important: If you performed this step while the JupyterLab server is running, you will need to restart it for the extension to take effect.

This function also takes the jupyter path as an optional argument. See the above subsection on installing on Jupyter notebooks for more description of the default behavior when this argument is omitted and pointers to troubleshooting information. tl;dr: if you launch JupyterLab via IJulia.jupyterlab(), run

using WebIO
WebIO.install_jupyter_labextension(condajl=true)

to force WebIO to be installed to the correct place.

Within the Atom text editor

If you have set up the Julia integration provided by Juno, evaluating any expression which returns an Interact-renderable object (such as a widget or the output of @manipulate) will show up in a plot-pane within the editor. No extra setup steps are required.

As a standalone web page

Any Julia function that returns an Interact-renderable object (such as a widget or the output of an @manipulate) can be repurposed to run as a simple web page served by the Mux web app framework.

using Mux, WebIO
function app(req) # req is a Mux request dictionary
 ...
end
webio_serve(page("/", app), 8000) # this will serve at http://localhost:8000/

Bonus: Publish something to Heroku!

Check out this repository which has a script to publish Interact app to Heroku with a simple command!

Example notebooks

A great resource to learn to use the various features of Interact is to try out the example notebooks and the tutorial in the doc/notebooks/ directory. Start up IJulia from doc/notebooks/:

using IJulia
notebook(dir=".")

Explore the wider widget ecosystem

Interact is built using WebIO which is a "hub" package that supports an ecosystem of composable web-based widgets that all share the same availability on the Julia front-ends.

Check out this introduction notebook about the WebIO ecosystem if you want to learn about how you can customize your UI using HTML, JavaScript and CSS. It will also get you started on writing your own widgets that others can use.

Learning more

To learn more, check out the documentation.