Streamlit Apache Reverse Proxy

Posted under » Apache » Python Data Analysis on 20 Jan 2022

After I had success in setting up Apache Reverse Proxy Mod for Jupyter Notebook. I thought I could do the same for Streamlit.

The first step is to create a virtual environment.

$ pip install streamlit

It will take a while and once done you can run streamlit. Test it.

$ streamlit hello

You will see that it runs on port 8501. You might want to change the port to make things more predictable. Try 8080

$ streamlit hello --server.port 8080

However, you do not have to put this parameter if you have modified the config.

As with Jupyter notebook, you need to enable the same exact Apache mods

The Apache config is similiar to Jupyter Notebook.

<VirtualHost *:80>
ServerName streamlit.com
ServerAdmin webmaster@localhost

ErrorLog ${APACHE_LOG_DIR}/error-streamlit.log
CustomLog ${APACHE_LOG_DIR}/access-streamlit.log combined

ProxyPreserveHost On
ProxyRequests Off

<Proxy *>
   Order deny,allow
   Allow from all
</Proxy>

   ProxyPass / "http://localhost:8080/
   ProxyPassReverse / "http://localhost:8080/

   <Location "/stream">
      ProxyPass ws://localhost:8080/stream
      ProxyPassReverse ws://localhost:8080/stream
   </Location>
</VirtualHost>

The thing to note that the websocket directory for streamlit is 'stream'.

If the streamlit hello app works fine, it means the proxy is working. So let us create a simple app.

Go and create a directory eg. /test and go to /test. vim pakistan.py

import pandas as pd
import streamlit as st

st.title("Welcome to Pakistan")
st.write("Our first Streamlit App")

st.write(
  pd.DataFrame({
   'A': [1, 2, 3, 4],
   'B': [5, 6, 7, 8]
	})
      )

To run pakistan.py

$ streamlit run pakistan.py --server.port 8080

If you use your browser to http://streamlit.com (eg) and you see your app running, then you haz success.

This may not do it if security is a concern. So let put password to access the page.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data