Skip to content

Instantly share code, notes, and snippets.

@andfanilo
Created March 21, 2022 18:11
Show Gist options
  • Save andfanilo/e4f80685a6cf333accd1cba1a1831eef to your computer and use it in GitHub Desktop.
Save andfanilo/e4f80685a6cf333accd1cba1a1831eef to your computer and use it in GitHub Desktop.
Star History but in Python with Streamlit + ghapi + Plotly Express
import pandas as pd
import plotly.express as px
import streamlit as st
from ghapi.all import GhApi
from ghapi.all import pages
@st.experimental_singleton
def load_ghapi() -> GhApi:
return GhApi(token = st.secrets["github"]["key"])
@st.experimental_memo
def check_repo(repo):
try:
username, repository = repo.split("/")
load_ghapi().activity.list_stargazers_for_repo(username, repository)
return True
except Exception:
return False
@st.experimental_memo
def search_stargazers(repo, per_page=30):
api = load_ghapi()
username, repository = repo.split("/")
api.activity.list_stargazers_for_repo(username, repository, per_page=per_page)
n_pages = api.last_page()
all_pages = pages(api.activity.list_stargazers_for_repo, n_pages, username, repository, per_page=per_page, headers={"Accept": "application/vnd.github.v3.star+json"})
data = [{"date": item["starred_at"], "count": 1} for page in all_pages for item in page]
df = pd.DataFrame(data)
df["date"] = pd.to_datetime(df["date"])
df = df.resample('W-Mon', on='date').sum().cumsum()
df["repo"] = repo
return df
st.title("Streamlit star-history")
st.sidebar.header("Configuraton")
all_repos = st.sidebar.text_area("Enter list of repos as username/repository")
if all_repos == "":
st.warning("Enter repository info")
st.stop()
for repo in all_repos.splitlines():
if not check_repo(repo):
st.warning(f"Repo {repo} does not seem to exist")
st.stop()
df = pd.concat([search_stargazers(repo) for repo in all_repos.rstrip().splitlines()])
fig = px.line(df, x=df.index, y="count", color="repo")
fig.update_layout(hovermode="x")
st.plotly_chart(fig)
@andfanilo
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment