First Steps #5: Pluto.jl 🎈
Pluto.jl provides a reactive notebook environment specific to Julia.
What's Pluto?
Notebook environments (e.g. Jupyter and Observable) have become extremely popularity in the last decade. Â They give programmers a way to intersperse code with markup, add interactive UI elements, and show off code in a format more interesting than text files. Â People love them (well, not everyone).
Pluto.jl is a newcomer (PlutoCon 2021 was just held to celebrate its one-year anniversary!) to the world of notebook environments. Â It provides a reactive environment specific to Julia. Â People are doing some very cool things with Pluto. Â Check out MIT's Introduction to Compuitational Thinking course for some fantastic public lectures with Pluto.
Pluto Quickstart
- Installing Pluto:
] add Pluto
- Starting the Pluto Server:
using Pluto
Pluto.run()
- The above command will open up the following page. Â
![Welcome](https://www.juliafordatascience.com/content/images/2021/04/Screen-Shot-2021-04-28-at-3.58.43-PM.png)
- To get back to this page from an opened notebook, click the Pluto.jl icon in the top navbar. Â
- For a deeper introduction to Pluto, go through the sample notebooks (we highly recommend them!).
- Press
ctrl + ?
to view keyboard shortcuts:
![](https://www.juliafordatascience.com/content/images/2021/04/Screen-Shot-2021-04-28-at-4.14.17-PM.png)
Key Points about Pluto
1. Your Code is Reactive. Â
When you change a variable, that change gets propagated through all cells which reference that variable.
![](https://www.juliafordatascience.com/content/images/2021/04/Apr-28-2021-16-05-47.gif)
2. Returned Values will Render as HTML.
That means things like the markdown string macro (md
) will look nice. Â Note that output gets displayed above the code.
![](https://www.juliafordatascience.com/content/images/2021/04/Screen-Shot-2021-04-28-at-4.21.32-PM.png)
3. Code can be Hidden.
Click the eye icon on the top left of a cell to hide the code. Â It only appears if your cursor is hovering over the cell.
![](https://www.juliafordatascience.com/content/images/2021/04/Apr-28-2021-16-22-56.gif)
4. You can @bind
HTML Inputs to Julia Variables.
Here we are using Pluto.@bind
along with the html
string macro to create a simple text input and bind it to a Julia variable my_input
. Â The @bind
macro works with any HTML input type.
![](https://www.juliafordatascience.com/content/images/2021/04/Apr-28-2021-16-28-24.gif)
5. You can Avoid Writing HTML by using PlutoUI.
- First:
] add PlutoUI
- Then:
![](https://www.juliafordatascience.com/content/images/2021/04/Apr-28-2021-16-33-07.gif)
- To see all of the UI options in PlutoUI, open the PlutoUI.jl sample notebook.
Notes, Tips, and Tricks
Multiple Expressions
- Pluto will try to get you to split multiple expressions into multiple cells (You can also put multiple expressions in a
begin
-end
block). Â This helps Pluto manage the dependencies between cells and avoids unnecessary re-running of code that "reacts" to something it doesn't need to.
Custom Display Methods
- If you want something to make use of Pluto's rich HTML display, you need to define your own
Base.show
method for thetext/html
MIME type.
![](https://www.juliafordatascience.com/content/images/2021/04/Screen-Shot-2021-04-28-at-4.46.10-PM.png)
Interpolating UI Elements into Markdown
You can use Julia's string interpolation syntax to interpolate values into a markdown block that will then get rendered as HTML. Â This includes html
strings and PlutoUI
elements! Â You can even define the UI element somewhere else to keep your markdown block look cleaner.
x_ui = @bind x Slider(1:10)
md"My UI Element: $x_ui"
# Provides the same result:
md"My UI Element: $(@bind x Slider(1:10))"
![](https://www.juliafordatascience.com/content/images/2021/04/Apr-28-2021-17-02-38.gif)
Final Thoughts
On a personal note, I've found Pluto particularly useful for making:
- Lightweight user interfaces for customers without strong Julia skills. Â I simply teach the customer to run
Pluto.run()
and then I don't need to deal with the overhead of developing a full web app. Â The downside is that Pluto notebooks can't (yet) be deployed as a web app. - Interactive presentations. Â Pluto works great for demonstrating code and more. Â A huge benefit is that thanks to reactivity, you'll never get in an awkward state with cells run out of order!
- Data Visualization. Â Data visualization is often an iterative process that takes many incremental changes to get the plot you want. Â The reactivity of Pluto provides instant feedback and greatly speeds up this process.
🚀 That's It!
You now know how to do some really cool stuff with Pluto. Â What will you build with it?
Enjoying Julia For Data Science? Â Please share us with a friend and follow us on Twitter at @JuliaForDataSci.
Additional Resources
- https://github.com/fonsp/Pluto.jl
- PlutoCon 2021
- Introduction to Computational Thinking - MIT
- Pluto's example notebooks!