Streamlit is a free and open-source framework that makes it easy to build and deploy web-based data science applications. In this tutorial, we will show you how to install Streamlit on Rocky Linux.
Before we begin, make sure you have a working Python 3 installation on your system. If not, you can refer to the Python documentation for installation instructions.
- Install Dependencies
To use Streamlit, you will need to install a few dependencies. These dependencies include the pip
package manager and the setuptools
and wheel
Python libraries.
To install these dependencies on Rocky Linux, you can use the dnf
command:
dnf install python3-pip python3-setuptools python3-wheel
Once the dependencies are installed, you can proceed to the next step.
- Install Streamlit
To install Streamlit, use the pip
command to install the streamlit
package:
pip3 install streamlit
This command installs the latest version of Streamlit from the Python Package Index (PyPI). You can also install a specific version of Streamlit by specifying the version number in the pip
command (e.g. pip3 install streamlit==0.71.0
).
Once the installation is complete, you can proceed to the next step.
- Create a Streamlit Application
The next step is to create a new Streamlit application. A Streamlit application is a Python script that defines the layout and functionality of your web application.
To create a new Streamlit application, create a new file named app.py
in a directory of your choice (e.g. /var/www/streamlit
). In the app.py
file, add the following code:
import streamlit as st
st.title('Hello, Streamlit!')
st.write('This is a simple Streamlit application.')
This code creates a new Streamlit application with a single page that contains a hello, Streamlit!
heading and a text element.
Save the app.py
file and exit the text editor.
- Run the Application
To run the Streamlit application, use the streamlit
command to start the Streamlit server:
streamlit run app.py
To complete the sentence, the following lines should be added to the app.py
file:
Copy code
if __name__ == '__main__':
st.run()
These lines ensure that the Streamlit application is only run when the app.py
script is executed directly, and not when it is imported as a module by another script.
Once you have added these lines to the app.py
file, save it and exit the text editor.
- Configure Apache2
To serve the Streamlit application from Apache2, you will need to create a new virtual host configuration file. This file should be located in the /etc/httpd/conf.d
directory, and should have a .conf
extension. For example, you could name the file streamlit.conf
.
In the virtual host configuration file, add the following contents:
<VirtualHost *:80>
ServerName streamlit.example.com
ProxyPreserveHost On
ProxyPass / <http://localhost:8501/>
ProxyPassReverse / <http://localhost:8501/>
</VirtualHost>
These lines configure Apache2 to proxy requests to the Streamlit application running on the default port (8501). When a client requests a URL on the virtual host (e.g. http://streamlit.example.com/
), Apache2 will forward the request to Streamlit and return the response to the client.
Once you have created the virtual host configuration file, save it and restart Apache2 to apply the changes:
systemctl restart httpd
To view the Streamlit application in your web browser, open http://streamlit.example.com
in a new tab. You should see the same Streamlit application that you saw when accessing it directly from the Streamlit server.
Here are a few examples of using Streamlit:
- Creating an interactive data visualization dashboard. Streamlit allows you to quickly and easily create beautiful and interactive plots and charts using popular libraries like Matplotlib and Plotly.
- Building a machine learning model. Streamlit provides a simple and intuitive API for training and evaluating machine learning models, and can automatically generate visualizations of the model’s performance and predictions.
- Creating a web-based tool for data cleaning and preprocessing. Streamlit’s rich text editor and input widgets make it easy to build a user-friendly interface for working with data, and its built-in cache mechanism allows you to efficiently perform complex transformations on large datasets.
- Creating an interactive report or document. Streamlit’s markdown support and rich media capabilities allow you to easily create dynamic and interactive documents that can be shared with others.
- Building a collaborative platform for data analysis. Streamlit’s built-in support for sharing and collaborating on applications allows you to quickly and easily build a platform for teams to work together on data analysis tasks.
Conclusion
In this tutorial, we have shown you how to install Streamlit on Rocky Linux. Streamlit is a powerful framework for creating web-based data science applications, and can be easily integrated with other Python libraries and frameworks. By following the steps in this tutorial, you can easily install and configure Streamlit on your own system and start creating data science applications.