Skip to content

Commit fea770d

Browse files
committed
Improve playground
1 parent b27b7b2 commit fea770d

File tree

5 files changed

+133
-17
lines changed

5 files changed

+133
-17
lines changed

playground/Dockerfile

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,6 @@ COPY ./resources/opyrator.tar.gz $RESOURCES_PATH/opyrator.tar.gz
206206
# hadolint ignore=DL3013
207207
RUN pip install $RESOURCES_PATH/opyrator.tar.gz
208208

209-
# Copy files into workspace
210-
COPY \
211-
resources/scripts/docker-entrypoint.py \
212-
resources/5xx.html \
213-
$RESOURCES_PATH/
214-
215209
# Copy playground app to demos folder
216210
COPY resources/playground_app.py $RESOURCES_PATH/demos/
217211
# Copy demos to demos folder
@@ -224,11 +218,18 @@ RUN \
224218
# Cleanup
225219
clean-layer.sh
226220

227-
COPY ./resources/scripts/install-demos.py $RESOURCES_PATH/install-demos.py
228-
RUN \
229-
python $RESOURCES_PATH/install-demos.py && \
230-
# Cleanup
231-
clean-layer.sh
221+
COPY ./resources/scripts/install-demos.py $RESOURCES_PATH/scripts/install-demos.py
222+
RUN python $RESOURCES_PATH/scripts/install-demos.py
223+
# TODO: No cleanup should be performed here, otherwise the huggingface models are missing
224+
# clean-layer.sh
225+
226+
# Copy files into workspace
227+
COPY \
228+
resources/scripts/docker-entrypoint.py \
229+
resources/5xx.html \
230+
$RESOURCES_PATH/
231+
232+
COPY resources/scripts/activate-analytics.py $RESOURCES_PATH/scripts/
232233

233234
# Configure nginx
234235
COPY resources/nginx/nginx.conf /etc/nginx/nginx.conf

playground/resources/playground_app.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
from opyrator.core import name_to_title
66

7-
st.title("Opyrator Examples")
7+
st.set_page_config(page_title="Opyrator Playground", page_icon=":arrow_forward:")
88

9-
DEFAULT_DEMO = "hello_world"
9+
st.title("Opyrator Examples")
1010

1111
CUSTOM_STREAMLIT_CSS = """
1212
div[data-testid="stBlock"] button {
@@ -26,6 +26,13 @@
2626
"""
2727
st.markdown(BADGES, unsafe_allow_html=True)
2828

29+
st.markdown(
30+
"Opyrator enables you to instantly turn a simple Python function into a powerful web service that includes a HTTP API and a graphical UI."
31+
+ " You can explore some examples below. "
32+
)
33+
34+
DEFAULT_DEMO = "hello_world"
35+
2936
demos = [str(dir) for dir in filter(os.path.isdir, os.listdir(os.curdir))]
3037

3138
title_to_demo = {}
@@ -81,7 +88,7 @@ def open_link(url: str, new_tab: bool = True) -> None:
8188
with st.beta_expander("Export this Opyrator"):
8289
col1, col2 = st.beta_columns([1, 2])
8390
with col1:
84-
st.button("📦 Export to ZIP")
91+
open_zip_export_feature = st.button("📦 Export to ZIP")
8592

8693
with col2:
8794
st.code(
@@ -90,7 +97,7 @@ def open_link(url: str, new_tab: bool = True) -> None:
9097

9198
col1, col2 = st.beta_columns([1, 2])
9299
with col1:
93-
st.button("🐳 Export to Docker")
100+
open_docker_export_feature = st.button("🐳 Export to Docker")
94101

95102
with col2:
96103
st.code(
@@ -103,3 +110,9 @@ def open_link(url: str, new_tab: bool = True) -> None:
103110

104111
if open_api:
105112
open_link(f"../{selected_demo}_api")
113+
114+
if open_docker_export_feature:
115+
open_link("https://github.com/ml-tooling/opyrator/issues/4")
116+
117+
if open_zip_export_feature:
118+
open_link("https://github.com/ml-tooling/opyrator/issues/3")
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
Copied from: https://github.com/jrieke/traingenerator
3+
4+
Update index.html from streamlit by
5+
- adding tracking code for Google Analytics
6+
- adding meta tags for search engines
7+
- adding meta tags for social preview
8+
WARNING: This changes your existing streamlit installation (specifically the file
9+
static/index.html in streamlit's main folder). It should only be called once after
10+
installation, so this file doesn't get cluttered!
11+
The tag from Google Analytics (G-XXXXXXXXXX) has to be stored in an environment variable
12+
GOOGLE_ANALYTICS_TAG (or in a .env file).
13+
"""
14+
15+
import os
16+
import sys
17+
18+
import streamlit as st
19+
20+
21+
def replace_in_file(filename, oldvalue, newvalue):
22+
"""Replace string in a file and optionally create backup_filename."""
23+
# Read in the file
24+
with open(filename, "r") as f:
25+
filedata = f.read()
26+
27+
# Replace the target string
28+
filedata = filedata.replace(oldvalue, newvalue)
29+
30+
# Write the file out again
31+
with open(filename, "w") as f:
32+
f.write(filedata)
33+
34+
35+
# Find path to streamlit's index.html.
36+
st_dir = os.path.dirname(st.__file__)
37+
index_filename = os.path.join(st_dir, "static", "index.html")
38+
39+
# Insert tracking code for Google Analytics.
40+
tag = os.getenv("GOOGLE_ANALYTICS_TAG")
41+
if not tag:
42+
print("No tag provided, analytics is deactivated")
43+
sys.exit(1)
44+
45+
tracking_code = f"""<!-- Global site tag (gtag.js) - Google Analytics --><script async src="https://www.googletagmanager.com/gtag/js?id={tag}"></script><script>window.dataLayer = window.dataLayer || []; function gtag(){{dataLayer.push(arguments);}} gtag('js', new Date()); gtag('config', '{tag}');</script>"""
46+
47+
clarity_tag = os.getenv("CLARITY_TAG")
48+
if clarity_tag:
49+
# Add clarity tracking code
50+
clarity_tracking_code = f"""
51+
<script type="text/javascript">
52+
(function(c,l,a,r,i,t,y){{
53+
c[a]=c[a]||function(){{(c[a].q=c[a].q||[]).push(arguments)}};
54+
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
55+
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
56+
}})(window, document, "clarity", "script", "{clarity_tag}");
57+
</script>
58+
"""
59+
60+
tracking_code += clarity_tracking_code
61+
62+
size_before = os.stat(index_filename).st_size
63+
replace_in_file(index_filename, "<head>", "<head>" + tracking_code)
64+
size_after = os.stat(index_filename).st_size
65+
66+
print("Inserted tracking code into:", index_filename)
67+
print("Size before:", size_before)
68+
print("Size after: ", size_after)
69+
70+
# Insert meta tags for search & social preview.
71+
# Older info but good summary: https://css-tricks.com/essential-meta-tags-social-media/
72+
# 2020 info: https://stackoverflow.com/questions/19778620/provide-an-image-for-whatsapp-link-sharing
73+
META_TAGS = """
74+
<!-- Meta tags for search engines -->
75+
<meta name="description" content="Python functions with superpowers. Instantly deploy simple functions with REST API, UI, and more.">
76+
<!-- Meta tags for social preview -->
77+
<meta property="og:title" content="Opyrator Playground">
78+
<meta property="og:description" content="Python functions with superpowers">
79+
<meta property="og:url" content="https://github.com/ml-tooling/opyrator">
80+
<meta property="og:site_name" content="Opyrator Playground">
81+
<meta name="twitter:image:alt" content="Opyrator Playground">
82+
"""
83+
84+
# <meta name="twitter:card" content="summary_large_image">
85+
# <meta property="og:image" content="https://github.com/jrieke/traingenerator/raw/main/docs/assets/social-preview-tiny.png">
86+
87+
size_before = os.stat(index_filename).st_size
88+
replace_in_file(index_filename, "<head>", "<head>" + META_TAGS)
89+
size_after = os.stat(index_filename).st_size
90+
91+
print("Inserted meta tags into:", index_filename)
92+
print("Size before:", size_before)
93+
print("Size after: ", size_after)

playground/resources/scripts/docker-entrypoint.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
from subprocess import call
34
from urllib.parse import quote, unquote
45

@@ -17,5 +18,12 @@
1718
shell=True,
1819
)
1920

21+
# Set up playground app analytics
22+
RESOURCES_PATH = os.getenv("RESOURCES_PATH", "/resources")
23+
ACTIVATE_ANALYTICS_SCRIPT = os.path.join(
24+
RESOURCES_PATH, "scripts", "activate-analytics.py"
25+
)
26+
call(f"{sys.executable} {ACTIVATE_ANALYTICS_SCRIPT}", shell=True)
27+
2028
# Run supervisor
2129
call("supervisord -n -c /etc/supervisor/supervisord.conf", shell=True)

playground/resources/scripts/install-demos.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import sys
33
from subprocess import call
44

5-
DEMO_DIRECTORY = os.path.join(os.getenv("RESOURCES_PATH", "/resources"), "demos")
5+
RESOURCES_PATH = os.getenv("RESOURCES_PATH", "/resources")
6+
DEMO_DIRECTORY = os.path.join(RESOURCES_PATH, "demos")
67
demos = [
78
d
89
for d in os.listdir(DEMO_DIRECTORY)
@@ -12,7 +13,7 @@
1213
for demo in demos:
1314
# Install requirements
1415
call(
15-
f"{sys.executable} -m pip install -r "
16+
f"{sys.executable} -m pip install --no-cache-dir -r "
1617
+ os.path.join(DEMO_DIRECTORY, demo, "requirements.txt"),
1718
shell=True,
1819
)

0 commit comments

Comments
 (0)