Skip to content

Commit 31a45b5

Browse files
committed
chore: wire up django and streamlit in an environment-dependent way
the django and streamlit apps are now wired up in a way that works both for a local dev environment as well as a Cloud deployment.
1 parent d4234df commit 31a45b5

File tree

7 files changed

+38
-18
lines changed

7 files changed

+38
-18
lines changed

copilot/web/manifest.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ type: Load Balanced Web Service
1010
http:
1111
# Requests to this path will be forwarded to your service.
1212
# To match all requests you can use the "/" path.
13-
path: '/'
13+
path: "/"
1414
# You can specify a custom health check path. The default is "/".
15-
healthcheck: '/healthcheck'
15+
healthcheck: "/healthcheck"
1616

1717
# Configuration for your containers and service.
1818
image:
@@ -23,24 +23,24 @@ image:
2323
# Port exposed through your container to route traffic to it.
2424
port: 8000
2525

26-
cpu: 256 # Number of CPU units for the task.
27-
memory: 512 # Amount of memory in MiB used by the task.
28-
platform: linux/x86_64 # See https://aws.github.io/copilot-cli/docs/manifest/lb-web-service/#platform
29-
count: 1 # Number of tasks that should be running in your service.
30-
exec: true # Enable running commands in your container.
26+
cpu: 256 # Number of CPU units for the task.
27+
memory: 512 # Amount of memory in MiB used by the task.
28+
platform: linux/x86_64 # See https://aws.github.io/copilot-cli/docs/manifest/lb-web-service/#platform
29+
count: 1 # Number of tasks that should be running in your service.
30+
exec: true # Enable running commands in your container.
3131
network:
3232
connect: true # Enable Service Connect for intra-environment traffic between services.
3333

3434
# storage:
35-
# readonly_fs: true # Limit to read-only access to mounted root filesystems.
35+
# readonly_fs: true # Limit to read-only access to mounted root filesystems.
3636

3737
# Optional fields for more advanced use-cases.
3838
#
39-
#variables: # Pass environment variables as key value pairs.
40-
# LOG_LEVEL: info
39+
variables: # Pass environment variables as key value pairs.
40+
STREAMLIT_HOST: streamlit.local
4141

42-
secrets: # Pass secrets from AWS Systems Manager (SSM) Parameter Store.
43-
DJANGO_ALLOWED_HOSTS: /pems/web/DJANGO_ALLOWED_HOSTS # The key is the name of the environment variable, the value is the name of the SSM parameter.
42+
secrets: # Pass secrets from AWS Systems Manager (SSM) Parameter Store.
43+
DJANGO_ALLOWED_HOSTS: /pems/web/DJANGO_ALLOWED_HOSTS # The key is the name of the environment variable, the value is the name of the SSM parameter.
4444

4545
# You can override any of the values defined above by environment.
4646
#environments:

pems/core/context_processors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33
"""
44

55
from pems import __version__
6+
from django.conf import settings
67

78

89
def pems_version(request):
910
"""Context processor adds information about the PeMS application's version."""
1011

1112
return {"pems_version": __version__}
13+
14+
15+
def streamlit_host(request):
16+
"""Context processor to add the Streamlit host URL part to the context."""
17+
18+
return {"streamlit_host": settings.STREAMLIT_HOST}

pems/districts/templates/districts/district.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ <h2>Form</h2>
1212
</div>
1313
<div class="row" style="min-height: 450px;">
1414
<div class="col-lg-12 border">
15-
<iframe class="streamlit-app" src="http://localhost:8501/stations--stations?embed=true&district_number={{ current_district.number }}">
15+
<iframe class="streamlit-app"
16+
src="http://{{ streamlit_host }}:8501/stations--stations?embed=true&district_number={{ current_district.number }}">
1617
</iframe>
1718
</div>
1819

pems/districts/templates/districts/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ <h2>Chart</h2>
2525
</div>
2626
<div class="row" style="min-height: 450px;">
2727
<div class="col-lg-12 border">
28-
<iframe class="streamlit-app" src="http://localhost:8501/stations--stations?embed=true">
28+
<iframe class="streamlit-app" src="http://{{ streamlit_host }}:8501/stations--stations?embed=true">
2929
</iframe>
3030
</div>
31-
3231
</div>
3332
</div>
3433
{% endblock districts-content %}

pems/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def _filter_empty(ls):
6161
"django.contrib.auth.context_processors.auth",
6262
"django.contrib.messages.context_processors.messages",
6363
"pems.core.context_processors.pems_version",
64+
"pems.core.context_processors.streamlit_host",
6465
],
6566
},
6667
},
@@ -138,3 +139,6 @@ def _filter_empty(ls):
138139
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
139140

140141
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
142+
143+
# Streamlit settings
144+
STREAMLIT_HOST = os.environ.get("STREAMLIT_HOST", "localhost")
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<h1>Streamlit inside Django</h1>
22

3-
<iframe
4-
src="http://localhost:8501/sample--bikeshare?embed=true"
5-
style="height: 100%; width: 100%;">
3+
<iframe src="http://{{ streamlit_host }}:8501/sample--bikeshare?embed=true" style="height: 100%; width: 100%;">
64
</iframe>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
from pems.core.context_processors import streamlit_host
3+
4+
5+
@pytest.mark.parametrize("host_name", ["localhost", "other.host"])
6+
def test_streamlit_host(app_request, settings, host_name):
7+
"""Test that the streamlit_host context processor returns the correct host."""
8+
settings.STREAMLIT_HOST = host_name
9+
context = streamlit_host(app_request)
10+
11+
assert context["streamlit_host"] == host_name

0 commit comments

Comments
 (0)