Sign inSign up

jysgro/streamlit

By jysgro

Updated almost 2 years ago

Ubuntu 22.04 with Python 3.10.12 as `python3` with streamlit pandas plotly-express

Image
Data science
0

197

jysgro/streamlit repository overview

Purpose: run a python script with streamlit code for display in a web browser.

Streamlit sends to port 8501 which is mapped to 8080 on the local computer, or choose another e.g. 8787 or 8888...

To run while sharing the current directory which should contain your code as e.g. app.py

docker run --rm -v ${PWD}:/workdir -w /workdir  -p 8080:8501 jysgro/streamlit:plotly streamlit run /workdir/app.py

Then go to a web browser with address: localhost:8080 ( NOT 8501 as suggested, this port is sent to 8080 on the host computer. Press together Control and C to stop the process.

Since the python script is on the local computer it is easy to switch to another one. As an example here is a suggested short script from streamlit documentation st.map we can call st-map-demo.py:

File: st-map-demo.py

import streamlit as st
import pandas as pd
import numpy as np

df = pd.DataFrame(
    {
        "col1": np.random.randn(1000) / 50 + 37.76,
        "col2": np.random.randn(1000) / 50 + -122.4,
        "col3": np.random.randn(1000) * 100,
        "col4": np.random.rand(1000, 4).tolist(),
    }
)

st.map(df, latitude="col1", longitude="col2", size="col3", color="col4")

As a test: save this file in your working directory as a plain text file called st-map-demo.py, and then launch with:

docker run --rm  -v ${PWD}:/workdir -w /workdir  -p 8080:8501 jysgro/streamlit:plotly streamlit run /workdir/st-map-demo.py

Then go to a web browser with address: localhost:8080

Press together Control and C to stop the process.

Dockerfile

Installing in Ubuntu 24.04 with Python got complicated so I am using Ububntu 22.04 to use pip

apt-utils qwas not recognized as a package and it commented out. And the non interactive option is added for this the image to be created without errors.

PIL is another python package suggested in the video that did not install, and does not exists as such, but it is not necessary for my purpose so it is also omitted.

I add numpy which can be helpful but also necessary for the short demo.

I do not use the COPY command with the purpose that the app.py is not saved within the image but is available by sharing the current directory (${PWD} above.)

I do not add a CMD command which may confuse things for other users. rather, the necessary commands are added on the docker run command as above.

 From info: How to Build and Run Streamlit App on Docker.
# https://youtu.be/08WUVeMRgH0
# FROM ubuntu:latest
FROM ubuntu:22.04
WORKDIR /usr/app/src
ARG LANG='en_us.UTF-8'
# Download and install dependencies
# https://stackoverflow.com/questions/51023312/docker-having-issues-installing-apt-utils
RUN DEBIAN_FRONTEND=noninteractive apt-get update \
    && apt-get install -y --no-install-recommends \
#    && apt-utils \
    # build essentials \
    locales \
    python3-pip \
    python3-yaml \
    rsyslog systemd systemd-cron sudo \
    && apt-get clean
    
# RUN pip3 install --upgrade pip
# RUN pip3 install streamlit pandas plotly-express PIL

RUN apt-get install python3-pip --upgrade pip
RUN pip install streamlit pandas plotly-express numpy

# COPY / ./
# Tell the image what to do when it starts as a container
# CMD ["streamlit", "run", "main.py"]

Building the image

Image was build for amd64 (Intel) and arm64 CPUs from the Dockerfile above with command:

docker build --platform=linux/amd64,linux/arm64 -t jysgro/streamlit:plotly .

Tag summary

Content type

Image

Digest

sha256:56583bcc0

Size

432.5 MB

Last updated

almost 2 years ago

docker pull jysgro/streamlit:plotly