-
Notifications
You must be signed in to change notification settings - Fork 10
/
🚧_Streamlit_Issue_Explorer.py
229 lines (197 loc) · 9.56 KB
/
🚧_Streamlit_Issue_Explorer.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import pathlib
import platform
import re
import requests
import streamlit as st
import streamlit.components.v1 as components
st.set_page_config(page_title="Streamlit Issue", page_icon="🚧")
st.title("🚧 Streamlit Issues")
st.caption(
f"""
A collection of Streamlit apps to replicate issues and bugs. Add your issue script [here](https://github.com/streamlit/st-issues).
Running with Python {platform.python_version()} and Streamlit {st.__version__}.
"""
)
DEFAULT_SELECTION = ""
DEFAULT_SCRIPT_NAME = "app.py"
DEFAULT_ISSUES_FOLDER = "issues"
def initial_query_params() -> dict:
"""
When page is first loaded, or if current params are empty, sync url params to
session state. Afterwards, just return local copy.
"""
if (
"initial_query_params_issue" not in st.session_state
or not st.session_state["initial_query_params_issue"]
):
st.session_state["initial_query_params_issue"] = st.query_params.to_dict()
return st.session_state["initial_query_params_issue"]
path_of_script = pathlib.Path(__file__).parent.resolve()
path_to_issues = pathlib.Path(path_of_script).joinpath(DEFAULT_ISSUES_FOLDER).resolve()
issues = []
for issue_folder in os.listdir(path_to_issues):
issue_folder_path = path_to_issues.joinpath(issue_folder).resolve()
if not issue_folder_path.is_dir():
continue
issue_script = issue_folder_path.joinpath(DEFAULT_SCRIPT_NAME).resolve()
if not issue_script.is_file():
continue
issues.append(issue_folder)
title_to_issue_folder = {}
issue_titles = []
default_index = 0
for i, issue in enumerate(issues):
if not issue:
continue
if "gh-template" in issue:
# Ignore template
continue
title_to_issue_folder[issue] = issue
issue_titles.append(issue)
# Add empty state:
issue_titles.sort(reverse=True)
issue_titles = [""] + issue_titles
query_params = initial_query_params()
if "issue" in query_params and query_params["issue"]:
query_param_issue = str(query_params["issue"])
if query_param_issue in issue_titles:
default_index = issue_titles.index(query_param_issue)
selected_issue = st.selectbox("Select Issue", options=issue_titles, index=default_index)
print("Selected issue:", selected_issue, flush=True)
st.query_params["issue"] = selected_issue
@st.cache_data(ttl=60 * 5) # cache for 5 minutes
def request_github_issue(issue_number: str) -> dict:
headers = {
'Authorization': 'token ' + st.secrets["github"]["token"]
}
try:
response = requests.get(
f"https://api.github.com/repos/streamlit/streamlit/issues/{issue_number}",
headers=headers,
timeout=100
)
if response.status_code == 200:
return response.json()
else:
print(f"Failed to retrieve data: {response.status_code}")
except Exception as ex:
print(ex, flush=True)
return None
def get_all_github_issues():
issues = []
page = 1
headers = {
'Authorization': 'token ' + st.secrets["github"]["token"]
}
while True:
try:
response = requests.get(
f"https://api.github.com/repos/streamlit/streamlit/issues?state=open&per_page=100&page={page}",
headers=headers
)
if response.status_code == 200:
return response.json()
else:
print(f"Failed to retrieve data: {response.status_code}")
break
except Exception as ex:
print(ex, flush=True)
break
return issues
if selected_issue:
selected_issue_folder = title_to_issue_folder[selected_issue]
selected_issue_folder_path = path_to_issues.joinpath(
selected_issue_folder
).resolve()
with st.container():
if (
selected_issue_folder.startswith("gh-")
and selected_issue_folder.replace("gh-", "").isnumeric()
):
issue_number = selected_issue_folder.replace("gh-", "")
# Request issue from GitHub API and extract the body:
try:
data = request_github_issue(issue_number)
if data:
if "title" in data:
issue_title = data["title"].strip()
st.markdown(f"**{issue_title}**")
BADGES = f"""
<a href="https://github.com/streamlit/streamlit/issues/{issue_number}" title="Issue State" target="_blank"><img src="https://img.shields.io/github/issues/detail/state/streamlit/streamlit/{issue_number}?style=flat-square"></a>
<a href="https://github.com/streamlit/streamlit/issues/{issue_number}" title="Issue Last Update" target="_blank"><img src="https://img.shields.io/github/issues/detail/last-update/streamlit/streamlit/{issue_number}?style=flat-square"></a>
<a href="https://github.com/streamlit/streamlit/issues/{issue_number}" title="Issue Created at" target="_blank"><img src="https://img.shields.io/github/issues/detail/age/streamlit/streamlit/{issue_number}?style=flat-square"></a>
<a href="https://github.com/streamlit/streamlit/issues/{issue_number}" title="Issue Comments" target="_blank"><img src="https://img.shields.io/github/issues/detail/comments/streamlit/streamlit/{issue_number}?style=flat-square"></a>
<a href="https://github.com/streamlit/streamlit/issues/{issue_number}" title="Issue Labels" target="_blank"><img src="https://img.shields.io/github/issues/detail/label/streamlit/streamlit/{issue_number}?style=flat-square&label=%20"></a>
"""
st.markdown(BADGES, unsafe_allow_html=True)
issue_body = data["body"]
if issue_body:
with st.expander("Issue Description"):
st.markdown(issue_body, unsafe_allow_html=True)
st.markdown("---")
st.caption(
"Add the following markdown badge to the issue on Github to provide a link to this app:"
)
st.code(
f"[![Open in Streamlit Cloud](https://static.streamlit.io/badges/streamlit_badge_black_white.svg)](https://issues.streamlitapp.com/?issue={selected_issue})",
language="markdown",
)
steps_to_reproduce = None
# New issue template
if "### Steps To Reproduce" in issue_body:
steps_to_reproduce = issue_body.split("### Steps To Reproduce")[
1
].split("### Is this a regression")[0].replace("###", "#####")
# Old issue template
elif "### Steps to reproduce" in issue_body:
# Extract from issue body
steps_to_reproduce = issue_body.split("### Steps to reproduce")[
1
].split("###")[0]
# Remove markdown code blocks via regex
steps_to_reproduce = re.sub(
r"```.*?```",
"```\nSee the code below...\n```",
steps_to_reproduce,
flags=re.DOTALL,
)
# Remove Streamlit badge
steps_to_reproduce = re.sub(
r"\[!\[Open in Streamlit Cloud\]\(https://static.streamlit.io/badges/streamlit_badge_black_white.svg\)\]\(https:.*?\)",
"",
steps_to_reproduce,
)
if steps_to_reproduce:
with st.expander("Steps to reproduce"):
st.markdown(steps_to_reproduce, unsafe_allow_html=True)
except Exception as ex:
print(ex, flush=True)
with st.expander("Source Code", expanded=True):
with open(
selected_issue_folder_path.joinpath(DEFAULT_SCRIPT_NAME).resolve(),
encoding="UTF-8",
) as f:
st.code(f.read(), language="python")
if (
selected_issue_folder_path.joinpath("requirements.txt").exists()
or selected_issue_folder_path.joinpath("Pipfile").exists()
):
st.info(
f"""
This issue script requires additional dependencies to run.
Therefore, it needs to be [deployed separately](https://share.streamlit.io/streamlit/st-issues/main/{DEFAULT_ISSUES_FOLDER}/{selected_issue_folder}/{DEFAULT_SCRIPT_NAME}).
In case the app embedded below is not running, you can deploy it yourself [here](https://share.streamlit.io/deploy?repository=streamlit/st-issues&branch=main&mainModule={DEFAULT_ISSUES_FOLDER}/{selected_issue_folder}/{DEFAULT_SCRIPT_NAME}).
""",
)
st.caption(
f"[Open in a new tab](https://share.streamlit.io/streamlit/st-issues/main/{DEFAULT_ISSUES_FOLDER}/{selected_issue_folder}/{DEFAULT_SCRIPT_NAME})"
)
# embed streamlit docs in a streamlit app
components.iframe(
f"https://share.streamlit.io/streamlit/st-issues/main/{DEFAULT_ISSUES_FOLDER}/{selected_issue_folder}/{DEFAULT_SCRIPT_NAME}?embed=true",
height=650,
)
else:
# Run the issue script
exec(open(selected_issue_folder_path.joinpath(DEFAULT_SCRIPT_NAME)).read())