Streamlit: A must learn tool for data Scientist
Streamlit is a Python-based web application framework for visualizing data and analyzing results in a more efficient and flexible way. It is an open source library that assists data scientists and academics to develop Machine Learning (ML) visualization dashboards in a short period of time. We can build and deploy powerful data applications with just a few lines of code.
Currently, real-world applications are in high demand and developers are developing new libraries and frameworks to make on-the-go dashboards easier to build and deploy. Streamlit is a library that reduces your dashboard development time from days to hours. Following are some reasons to choose the Streamlit:
- It is a free and open-source library.
- Installing Streamlit is as simple as installing any other python package
- It is easy to learn because you won’t need any web development experience, only a basic understanding of Python is enough to build a data application.
- It is compatible with almost all machine learning frameworks, including Tensorflow and Pytorch, Scikit-learn, and visualization libraries such as Seaborn, Altair, Plotly, and many others.
Required applications and packages:
We’ll need the following applications and packages to work with streamlit.
- Python — We need at least the python 3.7 version or greater.
- pip — We can install pip with the help of the terminal or using the code editor.
- Streamlit — We have to install the Streamlit library before launching any Streamlit application. Run the following command in the terminal to install streamlit.
Let’s create a basic application using streamlit.
- To create a basic streamlit application, create a new Python file with any name you want, such as app.py, and save it.
- Then use your installed IDE or text editor to open the newly created file.
- Use the following code In your empty python file.
4. Run the application by using the following command in the terminal.
5. The localhost hyperlink will appear in the terminal.Copy that link in the browser or just click on it to get the output, as demonstrated below:
Our first app’s output will be displayed in the browser as shown:
To perform visualization with streamlit we need a dataset and we’re using a dataset of penguins. The dataset can be downloaded by using the following link:
Let’s get started with importing the following libraries in our python file:
The next step is to import the dataset and write the results on the output screen using the following code:
The data’s head will be displayed on the output screen, and it will look like this:
Let’s start the plotting.
Plot a Line chart, Bar chart, and Area chart with Streamlit.
To plot the Line chart, Area chart, and Bar chart by using the Streamlit, use the following code. it will help to plot the flipper length and bill length of penguins.
Plotting with the help of Seaborn and Matplotlib
We can also use matplotlib and seaborn libraries for plotting with stramlit. The following code will help to plot a histogram of penguin’s flipper length:
Plotly is an open-source library that provides a list of chart types as well as tools with callbacks to make a dashboard. Let’s use Plotly to visualize our data. Import the Plotly library, and then we’ll use the following code to plot the histogram:
Bokeh is a Python data visualization library that generates fast interactive charts and plots. Let’s use the bokeh library to create a scatter plot, as shown below:
Altair is a Vega-Lite Python interface that allows you to specify Vega-Lite charts in Python. Using the Altair library, the following code will assist you in creating a bar chart:
Let’s plot an interesting scatter plotting by choosing the different categories from the penguin's dataset.
The following code will help to make a scatter plot by selecting several species of penguins and picking categories for the x-axis and y-axis from bill length, bill depth, flipper length, and body mass.
We can generate different scatter plots by selecting any category according to our preferences, as shown below.
This blog will help to provide information about streamlit from the initial steps. We have covered the topic of creating the first and simple application. We performed some sort of visualizations with the help of Plotly, Bokeh, and Altair libraries. This will help you to create other data applications.
Congratulations, you’re now ready to create your own Streamlit applications.