Can You Run a Streamlit App in Jupyter Environment? Let's Find Out:
Updated on
In the ever-evolving landscape of data science, two tools have emerged as game-changers for their ability to transform complex data into interactive, user-friendly applications: Streamlit and Jupyter. While they are separate tools, each with their own unique strengths, they can be used together in a workflow to develop and deploy interactive data applications.
Streamlit is a powerful tool for creating interactive data applications, while Jupyter is a versatile environment for interactive computing. Streamlit apps are not typically run directly within Jupyter. Instead, they are written as Python scripts and run from a command line where Streamlit is installed. This guide will delve into the depths of these tools, exploring their unique features, how they can be used together in a workflow, and how to develop and deploy Streamlit apps using Jupyter notebooks.
Have you heard of this awesome Data Analysis & Data Visualisation tool, that turns your Streamlit App into Tableau?
PyGWalker (opens in a new tab) is a Python Library that helps you easily embed a tableau-alternative UI into your own Streamlit app effortlessly.
Introduction to Streamlit and Jupyter
What is Streamlit?
Streamlit is an open-source Python library that allows data scientists and developers to create beautiful, interactive web applications in just a few lines of code. It's designed to handle data exploration, model training, and machine learning workflows with ease. Streamlit's simplicity and efficiency have made it a popular choice among data professionals.
What is Jupyter?
Jupyter is a versatile environment for interactive computing. It offers all the familiar building blocks of the classic Jupyter Notebook (notebook, terminal, text editor, file browser, rich outputs, etc.) in a flexible and powerful user interface. Jupyter enables you to work with documents and activities such as Jupyter notebooks, text editors, terminals, and custom components in a flexible, integrated, and extensible manner.
Developing Streamlit Apps in Jupyter Notebooks
While Streamlit and Jupyter are separate tools, they can be used together in a workflow. You can develop and preview your Streamlit apps within a Jupyter notebook using the streamlit-jupyter (opens in a new tab) package. This package allows you to use Streamlit widgets and components within your notebook for development and visualization purposes.
The Streamlit-Jupyter Package
The Streamlit-Jupyter package (opens in a new tab) is a simple Python package that allows you to preview and develop Streamlit apps within Jupyter notebooks. It provides Jupyter-compatible wrappers for Streamlit methods, allowing you to use Streamlit's interactive widgets and components within your notebook.
No problem, let's continue.
The Streamlit-Jupyter Package
The Streamlit-Jupyter package (opens in a new tab) is a simple Python package that allows you to preview and develop Streamlit apps within Jupyter notebooks. It provides Jupyter-compatible wrappers for Streamlit methods, allowing you to use Streamlit's interactive widgets and components within your notebook.
To use this package, you start by importing Streamlit and patching it with Streamlit-Jupyter:
import streamlit as st
from streamlit_jupyter import streamlit_patcher
streamlit_patcher.jupyter()
Now you can develop your notebook as usual, but with the added ability to use Streamlit's interactive features. This can be particularly useful for experimenting with different visualizations and components before converting your notebook to a Streamlit app.
Converting Jupyter Notebooks to Streamlit Apps
Once you've developed and previewed your Streamlit app within a Jupyter notebook, the next step is to convert your notebook to a Python script that can be run by Streamlit. This can be done using a tool called Jupytext, as demonstrated in this YouTube video (opens in a new tab).
Jupytext is a Python package that can convert Jupyter notebooks to and from different formats including scripts. It allows you to convert your notebook, with all its markdown cells and code cells, into a Python script that can be run as a Streamlit app.
Here's a simple example of how to use Jupytext to convert a notebook to a Python script:
jupytext --to py your_notebook.ipynb
This command will create a Python script named your_notebook.py
from your notebook your_notebook.ipynb
. You can then run this script as a Streamlit app using the streamlit run
command:
streamlit run your_notebook.py
Running Streamlit Apps
Streamlit apps are typically run from a command line where Streamlit is installed. You can run your Streamlit app by navigating to the directory containing your app's Python script and using the streamlit run
command followed by the name of your script:
streamlit run your_script.py
This will start the Streamlit server and open your default web browser to the URL of your app, typically http://localhost:8501
.
Conclusion
Streamlit and JupyterLab are powerful tools in the data science arsenal. While they each have their own unique strengths, they can also be used together in a workflow to develop interactive data applications. With the help of the Streamlit-Jupyter package, you can develop and preview Streamlit apps within Jupyter notebooks, and then use Jupytext to convert your notebooks to Python scripts that can be run as Streamlit apps.
Whether you're exploring data, building machine learning models, or creating interactive dashboards, Streamlit and JupyterLab offer a flexible and efficient way to turn your data into interactive applications.
Have you heard of this awesome Data Analysis & Data Visualisation tool, that turns your Streamlit App into Tableau?
PyGWalker (opens in a new tab) is a Python Library that helps you easily embed a tableau-alternative UI into your own Streamlit app effortlessly.
Frequently Asked Questions
Is Streamlit compatible with Jupyter notebook?
While Streamlit and Jupyter are separate tools, they can be used together in a workflow. You can develop and preview your Streamlit apps within a Jupyter notebook using the streamlit-jupyter package.
How do I deploy a Jupyter notebook on Streamlit?
You can convert your Jupyter notebook to a Python script using a tool like Jupytext, and then run this script as a Streamlit app using the streamlit run
command.
What is the difference between Jupyter and JupyterHub?
Jupyter is a versatile environment for interactive computing, while JupyterHub is a multi-user version of Jupyter that allows multiple users to use Jupyter notebooks, collaborate, and share resources.
What is similar to JupyterHub?
There are several tools similar to JupyterHub, including RStudio Server, Apache Zeppelin, and Databricks.
What is the difference between Streamlit and Jupyter Notebook?
Streamlit is a tool for creating interactive data applications, while Jupyter Notebook is an environment for interactive computing and data exploration. Streamlit apps are typically written as Python scripts and run from a command line, while Jupyter notebooks are web-based and allow for interactive coding and data visualization.
How do I run Streamlit in Jupyterlab?
While you can develop and preview Streamlit apps within a Jupyter notebook using the streamlit-jupyter package, Streamlit apps are typically run from a command line where Streamlit is installed, not directly within JupyterLab.