Qadry
unixcop Admin

How to Install Streamlit and deploy a Streamlit application on CentOS 8

Introduction

Streamlit is the fastest way to build and share data apps.

It is an open-source python library designed to create custom web applications for machine learning and data science.

This guide will explain how to run a Streamlit on a CentOS 8.

Just follow the steps below:

Update Python

We will use Python source code to install the latest version of Python3.

  • Install the required dependencies to update the Python source code.
yum groupinstall -y 'development tools'
yum install -y openssl-devel libffi-devel xz-devel wget bzip2-devel
  • Download the latest Python3.
[root@unixcop ~]# wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz
--2021-09-29 08:19:21--  https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz
Resolving www.python.org (www.python.org)... 199.232.80.223, 2a04:4e42:54::223
Connecting to www.python.org (www.python.org)|199.232.80.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25755357 (25M) [application/octet-stream]
Saving to: 'Python-3.9.7.tgz'

Python-3.9.7.tgz    100%[===================>]  24.56M   522KB/s    in 48s     

2021-09-29 08:20:10 (520 KB/s) - 'Python-3.9.7.tgz' saved [25755357/25755357]

[root@unixcop ~]#
  • Extract the downloaded tgz.
tar xvf Python-3.9.7.tgz
  • Change directory to Python-3.9.7 and Compile the python source code.
cd Python-3.9.7
./configure --enable-optimizations
  • Install Python 3.9.7.
make altinstall
  • Set Python 3.9.7 as default version.
alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.9 1 
alternatives --set python3 /usr/local/bin/python3.9 
echo "2" | alternatives --config python

Update pip

  • Update pip based on your updating Python to the latest version.
[root@unixcop Python-3.9.7]# /usr/local/bin/python3.9 -m pip install --upgrade pip
Requirement already satisfied: pip in /usr/local/lib/python3.9/site-packages (21.2.3)
Collecting pip
  Downloading pip-21.2.4-py3-none-any.whl (1.6 MB)
     |████████████████████████████████| 1.6 MB 553 kB/s 
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.2.3
    Uninstalling pip-21.2.3:
      Successfully uninstalled pip-21.2.3
Successfully installed pip-21.2.4
  • Set pip from Python 3.9. as default pip.
alternatives --install /usr/bin/pip pip /usr/local/bin/pip3.9 1 
alternatives --set pip /usr/local/bin/pip3.9
  • Check the current version of Python and pip.
[root@unixcop Python-3.9.7]# python --version
Python 3.9.7
[root@unixcop Python-3.9.7]# pip --version
pip 21.2.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
[root@unixcop Python-3.9.7]#

Install Streamlit

  • Using pip install the Streamlit.
pip install streamlit

Configure and Run Streamlit

You can run the streamlit with two methods:

Method (1): Run Streamlit with the Default Port

Streamlit runs on port 8501 by default. so that you need to allow it on the firewall.

firewall-cmd  --permanent --add-port 8501/tcp
firewall-cmd  --reload

When you run Streamlit on SSH Session. the Streamlit process will close when you exit the SSH Session.

So that run Streamlit using tmux which is a terminal multiplexer that allow you to run the Streamlit in the background.

  • Run the command below to create the TMUX session for streamlit:
tmux new -s StreamlitSession
  • Run your main python script. so run the following:
streamlit run example.py

Note: Change example.py to your python script file name.

  • Here we go, run Streamlit for the first time. then kindly enter your email address. or you can skip that.
  • To display your web application, Open your browser then visit http://IP_address:8501

Method (2): Run Streamlit on HTTP Port

  • Allow http port=80 on the firewall:
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
  • Run your python script with Streamlit:
streamlit run example.py --server.port 80

Note: Change example.py to your python script file name.

  • To display your web application, Visit http://IP_address

Deploy Streamlit app

  • To deploy Streamlit app, first we need to create one python file so run:
vim main.py
  • Add following in the main.py file:
import streamlit as st
st.text_input("Your name", key="name")

st.session_state.name
  • Save and exit.
  • Now, deploy the app on web using following command:
streamlit run main.py --server.port 80

  • To display your web application, Visit http://IP_address
  • as shown below in the screenshots.

    Conclusion

    In this article, we illustrated how to install Streamlit on CentOS 8.

    We have successfully completed with the installation process and also we have deployed our first app.

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here

    Latest articles

    x