-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
126 lines (105 loc) · 3.76 KB
/
Copy pathstreamlit_app.py
File metadata and controls
126 lines (105 loc) · 3.76 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import base64
from pathlib import Path
import streamlit as st
_logo_path = Path(__file__).parent / "groast-logo.jpg"
if _logo_path.exists():
_jpg_b64 = base64.b64encode(_logo_path.read_bytes()).decode()
_icon = f"data:image/jpeg;base64,{_jpg_b64}"
else:
_icon = "🛠️"
st.set_page_config(
page_title="Github-Roast | 悟空Github照妖镜",
page_icon=_icon,
layout="wide",
)
st.markdown(
"""
<style>
#MainMenu, .stDeployButton, [data-testid="stStatusWidget"], [data-testid="stSidebarNav"] { visibility: hidden; display: none; }
div[data-testid="stTextInput"] { min-width: 380px; }
</style>
""",
unsafe_allow_html=True,
)
from i18n import _
def _init():
defaults = {
"lang": "zh",
"scraped_user": None,
"scraped_repos": None,
"scraped_events": None,
"analysis_result": None,
"scrape_username": "",
}
for k, v in defaults.items():
if k not in st.session_state:
st.session_state[k] = v
_init()
from lib.sidebar import render_sidebar
render_sidebar()
title_col, title_col3 = st.columns([10, 5])
with title_col:
logo_html = ""
logo_path = Path(__file__).parent / "groast-logo.jpg"
if logo_path.exists():
b64 = base64.b64encode(logo_path.read_bytes()).decode()
logo_html = f'<img src="data:image/jpeg;base64,{b64}" style="width:42px;height:42px;vertical-align:middle;margin-right:10px;">'
st.markdown(
f"<h1 style='margin:0;display:flex;align-items:center;'>{logo_html}{_('title')}</h1>",
unsafe_allow_html=True,
)
with title_col3:
lang_opts = {"简体中文": "zh", "English": "en"}
idx = list(lang_opts.values()).index(st.session_state.lang)
sel = st.radio(
"Lang",
options=list(lang_opts.keys()),
index=idx,
horizontal=True,
label_visibility="collapsed",
)
if lang_opts[sel] != st.session_state.lang:
st.session_state.lang = lang_opts[sel]
st.rerun()
st.divider()
st.markdown(f"### {_('home_heading')}")
st.caption(_("home_desc"))
col_in, _col_spacer = st.columns([3, 1])
with col_in:
raw_input = st.text_input(
_("input_username_label"),
placeholder=_("input_username_placeholder"),
key="home_username",
label_visibility="collapsed",
).strip().lstrip("@").rstrip("/")
username = raw_input.removeprefix("https://github.com/").removeprefix("http://github.com/").removeprefix("github.com/")
# 如果是仓库链接 (user/repo),只取前面的用户名
username = username.split("/")[0]
col_btn, _btn_spacer = st.columns([1, 1])
with col_btn:
if st.button(_("btn_analyze"), type="primary", use_container_width=True):
if not username:
st.warning(_("warn_empty_username"))
else:
try:
from github_scraper import fetch_all
with st.spinner(_("spinner_scraping") + f" @{username}..."):
user, repos, events = fetch_all(username)
st.session_state.scraped_user = user
st.session_state.scraped_repos = repos
st.session_state.scraped_events = events
st.session_state.analysis_result = None
st.session_state.scrape_username = username
except Exception as e:
st.error(f"❌ {e}")
if st.session_state.get("scraped_user") is not None:
uname = st.session_state.scrape_username
repos = st.session_state.scraped_repos or []
events = st.session_state.scraped_events or []
st.success(
f"✅ {uname} — {len(repos)} repos, {len(events)} events"
)
if st.button(_("goto_analysis"), type="primary", use_container_width=True):
st.switch_page("pages/1_Analysis.py")
st.divider()
st.caption(_("footer"))