Build your Jupyter dashboard using Solara

콘텐츠

Welcome to the first part of a series of articles showing you how to create a dashboard in Jupyter and deploy it as a standalone web app. Importantly, you won't need to rewrite your app in a different framework for deployment. We will use a pure Python solution with no JavaScript or CSS required.

Jupyter notebooks are an incredible data analysis tool since they blend code, visualization, and narrative into a single document. However, we do not want to show the code if the insights must be presented to a non-technical audience.

Built on top of ipywidgets, the Solara framework integrates into the Jupyter Notebook, Jupyter Lab, and other Jupyter environments and is Open Source. As we will see in a later article, Solara apps can also be deployed efficiently using the Solara server. This, by itself, makes Solara a perfect solution for creating dashboards or data apps.

In this article, we will create a simple dashboard using Solara’s UI components in Jupyter. The final product will allow an end-user to filter, visualize, and explore a dataset on a map.

The end result will be a modern looking deploy app

You need to install pandas, matplotlib, folium and solara. Assuming you are using pip, you can execute on your shell:

Or in your notebook

The start

We will use a subsample of the San Fransisco crime dataset which contains information on types of crimes and where they were committed.

Download the CSV file to run this locally, or let the code below sort it out.

The first thing we do when we read the data is to print it out to see what the dataset contains.

Pandas printout of the Crime dataset Dataframe

The data looks clean, but since we will work with the Category and PdDistrict column data, let us convert those columns to title case.

Category and PdDistrict columns nicely formatted with title casing

Using proper software engineering practices, we write a function that filters a dataframe to contain only the rows that match our chosen districts and categories.

Now, with our filtered dataset, we create two bar charts. We use regular Pandas and Matplotlib, but Seaborn or Plotly are also appropriate choices.

Since we do not need bidirectional communication (e.g., we do not need to receive events or data from our map), we use Folium to display the locations of the committed crimes on a map. If we do need bidirectional communication, we can use ipyleaflet.

Since we cannot display all the data on the map without crashing your browser, we limit it to a maximum of 50 points.

Making our first reactive visualization

The above code works nicely, but if we want to explore different types of crimes, we need to modify and run all cells that determine our output manually. Would it not be much better to have a UI with controls determining the filtering and a view displaying the filtered data interactively?

Let's start by importing the solara package and creating three reactive variables.

A reactive variable is a container around a value (like an int, string, or list) that allows the UI to listen to changes automatically. Any change to your_reactive_variable.value will be picked up by Solara components that use them so that they can automatically redraw or update themselves.

Let us now create our first component (View), which filters the data based on the reactive variables and shows the map and the charts. Solara supports the display mechanism of Jupyter so that we can use our previously defined functions.

Our reactive visualization auto-updates when any reactive variable changes

Note that some UI parts (like the warning and the charts) are conditional. Solara will automatically find out what to add, remove, or update without you having to do this manually. Solara is declarative (similar to ReactJS) but also reactive. If we change the reactive variables, Solara will see those changes and notify the component instances that use its value.

If we run the next lines of code in our notebook, our View will automatically update.

We can now explore our data much faster since we don’t need to re-run the cells that depend on it.

Solara's reactive and declarative nature makes it scalable to much larger applications than regular ipywidgets, where keeping the UI in sync and adding, removing, and updating widgets is a manual and bug-prone process.

Adding controls

We created a declarative and reactive mini app in our notebook, but we still need to manually modify the values by executing a code cell in our Notebook. Now, let us create a UI to control it. All Solara input components support reactive variables. This means that controlling a reactive variable using a UI element is often a one-liner.

In one line of code we made our app filtering interactive by adding a dropdown

Whow, that was simple! We can now easily change the filter and see the results update. Lets do this for all our reactive variables, and put them into a single component.

All controls for the filtering of our dataframe and visualization

Note that the reactive variables are bi-directional, meaning that if you change it in the UI elements, it gets reflected on the Python code!

The final dashboard

We now have two parts of our UI in separate cells. This can be an amazing experience when developing in a notebook, as it flows naturally in the data exploration process while writing your notebook.

However, your end user will probably want something more coherent. The components we created are perfectly reusable, so we put them together in a single UI.

Our end result

Finally, our dashboard is finished!

Conclusions

Using Solara, you created an interactive dashboard within a Jupyter Notebook. Your Solara components are declarative, and when using reactive variables, they are also reactive. Whether you change reactive variables via code or the UI elements, your visualizations and maps update automatically.

Your dashboard prototype now runs in your Jupyter Notebook environment, but we can only deploy it as an app. In our next article, we will focus on deploying our notebook without making any code changes. In our third tutorial, we will expand our dashboard with a few more components and create a more advanced layout.

All documentation for Solara can be found at https://solara.dev, the GitHub repo can be found at https://github.com/widgetti/solara/ and feel free to join our Discord for a chat.

요약하다
This article introduces a series on creating and deploying a dashboard in Jupyter using the Solara framework, which allows for a pure Python solution without the need for JavaScript or CSS. Jupyter notebooks are effective for data analysis, but often require hiding code when presenting insights to non-technical audiences. Solara, built on ipywidgets, integrates seamlessly into Jupyter environments and enables the creation of interactive dashboards. The tutorial uses a subset of the San Francisco crime dataset to demonstrate how to filter, visualize, and explore data on a map. It covers the installation of necessary libraries and the creation of reactive visualizations that update automatically when data changes. The article emphasizes the ease of adding UI controls to filter data interactively, showcasing Solara's declarative and reactive nature. The final product is a cohesive dashboard that allows users to explore data without needing to rerun code cells. Future articles will focus on deploying the dashboard as a standalone app and enhancing its features. Documentation and resources for Solara are provided for further exploration.