Skip to content

Commit a1ed1d3

Browse files
authored
Refactor: pems_streamlit package (#184)
2 parents c1f561c + 9be9a63 commit a1ed1d3

File tree

27 files changed

+125
-76
lines changed

27 files changed

+125
-76
lines changed

.devcontainer/Dockerfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ ENV PATH="$PATH:/$USER/.local/bin" \
5050
PYTHONUSERBASE="/$USER/.local"
5151

5252
# install devcontainer requirements
53-
RUN pip install -e .[dev,test]
54-
53+
RUN pip install .[dev,test]
5554
# install docs requirements
5655
RUN pip install --no-cache-dir -r docs/requirements.txt
5756

58-
# install streamlit requirements
59-
RUN pip install --no-cache-dir -r streamlit_app/requirements.txt
57+
# install source packages as editable
58+
RUN pip install -e ./pems_streamlit -e ./pems_web

.github/workflows/tests-pytest.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ jobs:
2525

2626
- name: Install Python dependencies
2727
run: |
28-
pip install -e .[test]
29-
pip install -r streamlit_app/requirements.txt
28+
pip install .[test] ./pems_streamlit ./pems_web
3029
3130
- name: Run tests
3231
run: ./tests/pytest/run.sh

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"type": "debugpy",
3434
"request": "launch",
3535
"module": "streamlit",
36-
"args": ["run", "streamlit_app/main.py"],
36+
"args": ["run", "pems_streamlit/src/pems_streamlit/main.py"],
3737
"env": {
3838
"PYTHONBUFFERED": "1",
3939
"PYTHONWARNINGS": "default",
@@ -45,7 +45,7 @@
4545
"type": "debugpy",
4646
"request": "launch",
4747
"module": "streamlit",
48-
"args": ["run", "streamlit_app/main.py"],
48+
"args": ["run", "pems_streamlit/src/pems_streamlit/main.py"],
4949
"env": {
5050
"PYTHONBUFFERED": "1",
5151
"PYTHONWARNINGS": "default",

compose.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,11 @@ services:
6969
streamlit:
7070
build:
7171
context: .
72-
dockerfile: streamlit_app/Dockerfile
72+
dockerfile: pems_streamlit/container/Dockerfile
7373
image: caltrans/pems:streamlit
7474
env_file: .env
7575
ports:
7676
- "${STREAMLIT_LOCAL_PORT:-8501}:8501"
77-
volumes:
78-
- ./:/caltrans/app
7977

8078
volumes:
8179
pgdata:

infra/copilot/streamlit/manifest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ http:
1717
image:
1818
# Docker build arguments. For additional overrides: https://aws.github.io/copilot-cli/docs/manifest/backend-service/#image-build
1919
build:
20-
dockerfile: ../streamlit_app/Dockerfile
20+
dockerfile: ../pems_streamlit/container/Dockerfile
2121
context: ../
2222
# Port exposed through your container to route traffic to it.
2323
port: 8501

pems_streamlit/container/Dockerfile

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
ARG PYTHON_VERSION=3.12
2+
3+
# multi-stage build
4+
#
5+
# stage 1: builds the package wheel from source
6+
# using the git metadata for version info
7+
FROM python:${PYTHON_VERSION} AS build_wheel
8+
WORKDIR /build
9+
10+
# upgrade pip, install build dependencies
11+
RUN python -m pip install --upgrade pip build setuptools_scm
12+
13+
# copy source files
14+
COPY . .
15+
RUN git config --global --add safe.directory /build
16+
17+
# Move into directory to run the build
18+
WORKDIR /build/pems_streamlit
19+
20+
# build package
21+
RUN python -m build
22+
23+
# multi-stage build
24+
#
25+
# stage 2: installs the wheel in a fresh base container
26+
# using the pre-built package, and copying only needed source
27+
FROM python:${PYTHON_VERSION} AS app_container
28+
29+
ENV PYTHONDONTWRITEBYTECODE=1 \
30+
PYTHONUNBUFFERED=1 \
31+
USER=caltrans
32+
33+
EXPOSE 8501
34+
35+
# create non-root $USER and home directory
36+
RUN useradd --create-home --shell /bin/bash $USER && \
37+
python -m pip install --upgrade pip
38+
39+
COPY LICENSE LICENSE
40+
41+
# switch to non-root $USER
42+
USER $USER
43+
44+
# update env for local pip installs
45+
# see https://docs.python.org/3/using/cmdline.html#envvar-PYTHONUSERBASE
46+
# since all `pip install` commands are in the context of $USER
47+
# $PYTHONUSERBASE is the location used by default
48+
ENV PATH="$PATH:/$USER/.local/bin" \
49+
PYTHONUSERBASE="/$USER/.local" \
50+
PYTHONPATH="$PYTHONPATH:/$USER/app"
51+
52+
WORKDIR /$USER/app
53+
54+
COPY .streamlit .streamlit
55+
COPY pems_streamlit/container/entrypoint.sh entrypoint.sh
56+
COPY pems_streamlit/container/run.py run.py
57+
COPY --from=build_wheel /build/pems_streamlit/dist /wheels
58+
59+
RUN pip install $(find /wheels -name pems_streamlit*.whl)
60+
61+
ENTRYPOINT ["./entrypoint.sh"]

streamlit_app/entrypoint.sh renamed to pems_streamlit/container/entrypoint.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ BASE_URL_PATH=${STREAMLIT_BASE_URL:-""}
77
echo "Starting Streamlit with base URL path: ${BASE_URL_PATH}"
88

99
# Execute the Streamlit command, passing the base URL path
10-
exec streamlit run streamlit_app/main.py \
10+
# Use the run.py helper since streamlit run wants the path to a python file
11+
exec streamlit run run.py \
1112
--server.port=8501 \
1213
--server.address=0.0.0.0 \
1314
--server.baseUrlPath="${BASE_URL_PATH}"

pems_streamlit/container/run.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python
2+
import runpy
3+
4+
# since we're installing pems_streamlit as a package now, the main.py file won't exist
5+
# in the running container, so we can't point `streamlit run` at it
6+
7+
# instead, use runpy to run the pems_streamlit.main module directly.
8+
9+
if __name__ == "__main__":
10+
runpy.run_module("pems_streamlit.main", run_name="__main__", alter_sys=True)

pems_streamlit/pyproject.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[project]
2+
name = "pems_streamlit"
3+
description = "The Streamlit application for PeMS data visualizations."
4+
dynamic = ["version"]
5+
requires-python = ">=3.12"
6+
dependencies = [
7+
"boto3==1.39.7",
8+
"django==5.2.3",
9+
"pandas==2.3.0",
10+
"streamlit==1.45.1",
11+
]
12+
13+
[build-system]
14+
requires = ["setuptools>=75", "setuptools_scm>=8"]
15+
build-backend = "setuptools.build_meta"
16+
17+
[tool.setuptools.packages.find]
18+
where = ["src"]
19+
include = ["pems_streamlit*"]
20+
namespaces = false
21+
22+
[tool.setuptools.package-data]
23+
pems_streamlit = ["*.txt"]
24+
25+
[tool.setuptools_scm]
26+
# Tell scm to look one directory up for the .git folder
27+
root = ".."

0 commit comments

Comments
 (0)