Posts Art
Mark Topacio
Photo by Stephen Dawson on Unsplash
Interactive Dashboards in Python 2023
Written on 2023-07-08

Interactive dashboards are revolutionizing the way data is explored, analyzed, and presented. In the world of data-driven decision-making, these powerful tools provide an intuitive and engaging interface for users to interact with complex data sets and gain valuable insights. Python, being one of the most popular programming languages for data analysis and visualization, offers a range of libraries and frameworks to create interactive dashboards. In this post, we will explore some of the top Python libraries for building interactive dashboards. Whether you are a data scientist, a business analyst, or a developer, these Python libraries will empower you to create compelling visualizations and drive data-driven decision-making.

Streamlit

Streamlit has garnered a lot of attention since they were purchased by Snowflake a little over a year ago, and rightly so. It occupies the space right above Jupyter Notebooks in going from code to plots, forms, and widgets relatively easily with minimal code. Compared to the other libraries on this list, the API is extremely simple and intuitive. Knowledge of React is needed to create custom components since it’s used under the hood. If you don’t want to go that route, just pick one from many built from a very active community. They also have Community Cloud where you can deploy, manage, and share your apps for free.

Streamlit

The biggest drawbacks when using Streamlit.

  • There is limited customization, specifically styling things with CSS. You can hijack styling some of the elements by using their id; however, that may not be the best form.
  • Streamlit is mainly a server-side application. There is a custom component that accepts a string of Javascript.
  • Data changes trigger a whole page rerun. There are ways to mitigate this though.

That said, I still use it to prototype since it offers the fastest development time.

Solara

Solara is another Python library using React under the hood. However, unlike Streamlit which renders each React component independent and ties it to a Streamlit call, Solar is based on Reacton, which is a Python wrapper around the React framework. This benefits this framework by allowing the rerun of only affected components versus the whole page in Streamlit.

Solara

Solara has a syntax with a simplicity and intuitiveness comparable to Streamlit.

Solara Example

Example Solara app

Plotly Dash

Plotly Dash is an interactive dashboard framework built on Flask, Plotly.js, and React.js. It provides a high-level declarative syntax and supports interactive components like graphs, tables, and dropdowns. There is an enterprise edition that offers additional features; however, the active community provides a number of additional components libraries. Dash mainly uses server-side rendering like the previous entries, but it also includes the ability to ship Javascript in what they refer to as clientside-callbacks. This allows you to save the back-and-forth latency between data transformations by allowing your client to handle all the manipulations and filterings. It can even open your dashboard to use other Javascript libraries without creating a custom component. Like Streamlit, creating components requires an understanding of the React lifecycle. In general, though, callbacks are how Dash creates its reactive elements.

Dash Code

The class HTML allows you to structure your markup as if you were developing in HTML.

Dash App

Simple example of a Dash app

Another Dash App

Another example of a more involved Dash app

Notice the pseudo-HTML in the first image made available by the HTML class. One of the parameters is style which lets you add in-line styling if need be. Part of that is the ability to use classes that you can take from TailwindCSS. The only thing I wish is the plots had more customization. You can when using Plotly.js directly; however, it’s lacking some options when creating plots in Python.

Shiny

Yup, Posit (formerly RStudio) has made available their dashboard software in Python.

Shiny

Minus the whole R vs Python nuances, the syntax project structure feel very similar if you've used it in R.

I haven’t had the chance to use this in Python as this and Dash occupy very similar spaces. Instead of React, however, Shiny uses jQuery to create its reactive values. I also like that their HTML interface allows the use of CSS files, which lets me use Tailwind.

Shiny Example

Example of a Shiny app

Datasette-Dashboards

Datasette is a tool for exploring and publishing data. I’ve never had the chance to use this tool; however, their approach is really interesting. It aims to make the whole ordeal of interacting with data streamlined and accessible. Like the other tools in this list, it allows the user to take data, analyze it, and publish it as an interactive website. Where it differs is that it not only opens the data sets as API endpoints, but also lets you edit the queries that influence the visualizations. The datasette-dashboards plugin is still an experimental feature; however, it stays true to its approach by using YAML to simplify the code needed to create these plots.

yaml for plot

Example YAML used to create a dashboard.

Alt text

Example dashboard.

The underlying visualization libraries are Vega/Vega-Lite. Datasette can be deployed to traditional and serverless hosting platforms.

Web Frameworks

The last option is a catch-all for all the available web frameworks like Django, Flask, and all their async derivatives. These work by either developing a complete web page in HTML/CSS/JS or using that markup along Jinja to create a template. This is the current method I prefer alongside Astro, which opens up the door to both Javascript visualization libraries like Echarts.js and client-side data manipulation and computation.

Conclusion

My tooling went full circle to arrive back at Flask, which was my first introduction to creating dashboards with Python. Easier tooling like Streamlit and Dash made it possible to side-step all that development time to a deliverable at the cost of some customization and client-side computation. The look largely feels the same since it’s either built on React one way or another, or it uses the more common component libraries that are ported over to Python. My endgame as of late has been the use of Astro in combination with Echarts to easily create these static sites I can integrate with an API.

Honorable Mentions

Another avenue to create interactive visualizations is to use Jupyter Notebook alongside a visualization package. Some Python libraries that might help you out here are: