forked from streamlit/core-previews
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE2E_Tester_🧪.py
More file actions
103 lines (78 loc) · 3.17 KB
/
Copy pathE2E_Tester_🧪.py
File metadata and controls
103 lines (78 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from urllib.parse import urlparse
import streamlit as st
from execbox import execbox
from e2e_loader import get_script, get_scripts, select_script
with open("requirements.txt") as requirements:
s3_url = requirements.read().split("\n")[-2]
import re
def get_first_match(regex, s):
match = re.search(regex, s)
return match.group(1) if match else None
def get_branch_info():
path = urlparse(s3_url).path
path_parts = list(filter(None, path.split("/")))
core_preview_branch = "/".join(path_parts[:-1])
branch = get_first_match("(.*)-preview", core_preview_branch)
# There's no nightly branch, so default to develop
branch = "develop" if branch == "nightly" else branch
pr = get_first_match("pr-(\\d+)", core_preview_branch)
return branch, pr
def strip_license_header(script_content):
"""Remove license header comments from the beginning of the script."""
lines = script_content.split("\n")
first_non_comment_line = 0
# Find the first non-comment line
for i, line in enumerate(lines):
stripped = line.strip()
if not stripped.startswith("#") and stripped:
first_non_comment_line = i
break
# Return script without the header comments
return "\n".join(lines[first_non_comment_line:])
def remove_page_config(script_content):
"""Remove st.set_page_config(...) calls from the script."""
# This regex matches st.set_page_config with any arguments and across multiple lines
pattern = re.compile(r"st\.set_page_config\s*\([^)]*\)", re.DOTALL)
return pattern.sub("", script_content)
branch, pr_number = get_branch_info()
st.write(
f"🔠[View PR on Github](https://github.com/streamlit/streamlit/pull/{pr_number}) - "
if pr_number is not None
else "",
f"🎡 [Download wheel]({s3_url})",
)
col1, col2 = st.columns([6, 1], vertical_alignment="bottom", gap="small")
if col2.button(":material/refresh:", use_container_width=True):
get_script.clear()
get_scripts.clear()
# Get script name from query parameter if available
query_params = st.query_params.to_dict()
script_from_query = query_params.get("script", None)
with col1:
selected_script = select_script(branch, pr_number, default_script=script_from_query)
# If we have a script from query params, update the URL to match the selection
if selected_script and script_from_query != selected_script.get("name"):
st.query_params["script"] = selected_script.get("name", "")
# fallback if things fail
script = "import streamlit as st"
if selected_script:
if "download_url" in selected_script:
script = get_script(selected_script["download_url"])
# Strip license header from downloaded scripts
script = strip_license_header(script)
# Remove st.set_page_config calls
script = remove_page_config(script)
else:
with open(selected_script["path"], "r", encoding="utf-8") as f:
script = f.read()
if not bool(st.secrets.get("disable_editor", False)):
st.subheader("Edit my source :material/edit_square:")
execbox(
script,
autorun=True,
line_numbers=True,
height=300,
)
else:
# Run script directly
exec(script)