Skip to content

Commit 58cd074

Browse files
authored
fetch github stars count (#1084)
1 parent dfee852 commit 58cd074

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

pcweb/components/docpage/navbar/buttons/github.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import reflex as rx
2-
from pcweb.constants import GITHUB_STARS, GITHUB_URL
2+
from pcweb.constants import GITHUB_URL
33
from pcweb.components.icons.icons import get_icon
4+
from pcweb.github import GithubStarState
45

56

67
def github() -> rx.Component:
78
return rx.link(
89
rx.flex(
910
get_icon(icon="github_navbar", class_name="shrink-0 !text-slate-9"),
1011
rx.text(
11-
f"{GITHUB_STARS/1000:.1f}K",
12+
GithubStarState.stars,
1213
class_name="font-small",
1314
),
1415
class_name="text-slate-9 flex-row gap-2 hover:bg-slate-3 flex justify-center rounded-[10px] border border-slate-5 bg-slate-1 transition-bg cursor-pointer shadow-large py-0.5 px-3 items-center h-8",

pcweb/github.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Github stars count for the reflex repository."""
2+
3+
import asyncio
4+
import httpx
5+
6+
import reflex as rx
7+
import contextlib
8+
9+
REFLEX_STAR_COUNT = 0
10+
GITHUB_API_URL = "https://api.github.com/repos/reflex-dev/reflex"
11+
12+
13+
async def fetch_count():
14+
"""Fetch the stars count of the reflex repository."""
15+
try:
16+
while True:
17+
with contextlib.suppress(Exception):
18+
global REFLEX_STAR_COUNT
19+
data = httpx.get(GITHUB_API_URL).json()
20+
count = int(data["stargazers_count"])
21+
REFLEX_STAR_COUNT = round(count / 1000)
22+
await asyncio.sleep(3600)
23+
except asyncio.CancelledError:
24+
pass
25+
26+
27+
class GithubStarState(rx.State):
28+
@rx.var(cache=True, interval=60)
29+
def stars(self) -> str:
30+
return f"{REFLEX_STAR_COUNT}K"

pcweb/pcweb.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import reflex as rx
77
from pcweb import styles
8+
from pcweb.github import fetch_count
89
from pcweb.pages import page404, routes
910
from pcweb.pages.docs import outblocks, exec_blocks
1011
from pcweb.whitelist import _check_whitelisted_path
@@ -121,8 +122,10 @@
121122
("/docs/tutorial/adding-state", "/docs/getting-started/chatapp-tutorial"),
122123
("/docs/tutorial/final-app", "/docs/getting-started/chatapp-tutorial"),
123124
("/docs/getting-started/configuration", "/docs/advanced-onboarding/configuration"),
124-
("/docs/getting-started/how-reflex-works", "/docs/advanced-onboarding/how-reflex-works"),
125-
125+
(
126+
"/docs/getting-started/how-reflex-works",
127+
"/docs/advanced-onboarding/how-reflex-works",
128+
),
126129
# Recipes
127130
("/docs/recipes/auth", "/docs/recipes"),
128131
("/docs/recipes/auth", "/docs/recipes"),
@@ -135,6 +138,9 @@
135138
]
136139

137140
for source, target in redirects:
138-
app.add_page(lambda: rx.fragment(), route=source, on_load=rx.redirect(target))
141+
if _check_whitelisted_path(target):
142+
app.add_page(lambda: rx.fragment(), route=source, on_load=rx.redirect(target))
139143

140144
app.add_custom_404_page(page404.component)
145+
146+
app.register_lifespan_task(fetch_count)

0 commit comments

Comments
 (0)