In this tutorial, we are going to learn how to install Streamlit on Ubuntu 22.04
Streamlit is an open-source Python library that makes it easy to create and share beautiful, custom web apps for machine learning and data science. This is a faster way to build and share data apps. Streamlit turns data scripts into shareable web apps in minutes.
You don’t need to write the backend or frontend to define Streamlit, you only need to add widgets which is the same as declaring variables. This makes it easy to use and also deploy. Deploying your app is just a click of a button and your application is in the cloud.
Prerequisites
- Have PIP installed
- Python 3.7 and above
- IDE
Something to note here is, have a virtual environment installed so that Streamlit doesn’t impact any other projects you might be working on.
Install Streamlit on Ubuntu 22.04
1. Create a virtual environment
First, we need to create a virtual environment so that we can separate our Streamlit work from others. To create a virtual environment using the following command. First create a project directory.
$ sudo mkdir streamlit
$ cd streamlit
$ python3 -m venv env # env is the name of the virtual environment.
After creating a virtual environment, you can then open this project from your favorite IDE or you can continue from the terminal.
To activate the virtual environment, use the following command.
$ source env/bin/activate
If it happens you don’t have pip installed, you can do install using the following command.
sudo apt-get install python3-pip
After this we need to install pipenv
on our virtual environment.
$ pip3 install pipenv
This will create pipfile
in your workspace.
2. Install streamlit
To install Streamlit, use the following command on your terminal.
pip install streamlit
You will be in a position to see an output such as this one.
Collecting streamlit
Downloading streamlit-1.12.2-py2.py3-none-any.whl (9.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.1/9.1 MB 118.6 kB/s eta 0:00:00
Collecting protobuf<4,>=3.12
Downloading protobuf-3.20.1-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 88.4 kB/s eta 0:00:00
Collecting blinker>=1.0.0
Downloading blinker-1.5-py2.py3-none-any.whl (12 kB)
Collecting python-dateutil
Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting importlib-metadata>=1.4
Downloading importlib_metadata-4.12.0-py3-none-any.whl (21 kB)
Collecting packaging>=14.1
Using cached packaging-21.3-py3-none-any.whl (40 kB)
Collecting watchdog
Downloading watchdog-2.1.9-py3-none-manylinux2014_x86_64.whl (78 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 78.4/78.4 KB 202.5 kB/s eta 0:00:00
We can test whether the installation worked, let’s create an hello file from the terminal
streamlit hello
This will automatically open a web browser on localhost:8501
.
Create Streamlit App
Let’s create a simple example. Paste the following code on your browser and name it app.py
import streamlit as st
st.write('Hello world')
To run this code go to your terminal and type streamlit run app.py
streamlit run app.py
The output will be an hello world.
Let’s find the square of different numbers.
x = st.slider('x')
st.write(x, 'squared is', x * x)
You will get a slider as an output.
Conclusion
We have successfully installed Streamlit on ubuntu 22.04 and we have learned how to create sliders. This is a very important tool when it comes to data science. Feel free to consult Streamlit documentation.