Spaces:
Running
Running
Ok
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@ import requests
|
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
5 |
|
6 |
-
|
7 |
class DownloadsTracker:
|
8 |
def __init__(self):
|
9 |
self.all_packages = {}
|
@@ -27,8 +26,6 @@ class DownloadsTracker:
|
|
27 |
df["date"] = pd.to_datetime(df["date"])
|
28 |
df = df.sort_values("date")
|
29 |
df["cumulative_downloads"] = df["downloads"].cumsum()
|
30 |
-
# Add weekly downloads
|
31 |
-
df["weekly_downloads"] = df["downloads"].rolling(window=7).mean()
|
32 |
self.all_packages[pkg] = df
|
33 |
except:
|
34 |
errors.append(f"Error fetching {pkg}")
|
@@ -36,54 +33,25 @@ class DownloadsTracker:
|
|
36 |
return "\n".join(errors)
|
37 |
return None
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def plot(self, use_log_scale):
|
40 |
-
|
41 |
-
return px.line(title="No data to display")
|
42 |
-
|
43 |
-
# Prepare data
|
44 |
-
df_list = []
|
45 |
for pkg in self.current_packages:
|
46 |
if pkg in self.all_packages:
|
47 |
-
df = self.all_packages[pkg]
|
48 |
-
df[
|
49 |
-
df_list.append(df)
|
50 |
-
|
51 |
-
combined_df = pd.concat(df_list)
|
52 |
-
|
53 |
-
# Create subplot figures
|
54 |
-
fig1 = px.line(combined_df, x="date", y="cumulative_downloads", color="package", title="Package Downloads")
|
55 |
-
fig2 = px.line(combined_df, x="date", y="weekly_downloads", color="package")
|
56 |
-
|
57 |
-
# Combine into subplots
|
58 |
-
fig = px.line()
|
59 |
-
fig.add_traces(fig1.data)
|
60 |
-
fig.add_traces(fig2.data)
|
61 |
|
62 |
-
# Update layout for subplots
|
63 |
-
fig.update_layout(
|
64 |
-
height=800,
|
65 |
-
grid={'rows': 2, 'columns': 1},
|
66 |
-
title_text="Package Downloads"
|
67 |
-
)
|
68 |
-
|
69 |
-
# Move second set of traces to lower subplot
|
70 |
-
for i in range(len(fig2.data)):
|
71 |
-
fig.data[i + len(fig1.data)].update(yaxis='y2')
|
72 |
-
|
73 |
-
# Update scales
|
74 |
if use_log_scale:
|
75 |
fig.update_yaxes(type="log")
|
76 |
else:
|
77 |
fig.update_yaxes(type="linear")
|
78 |
-
|
79 |
return fig
|
80 |
|
81 |
-
def render(self, package_list, use_log_scale):
|
82 |
-
package_list = [p.strip() for p in package_list.split(",") if p.strip()]
|
83 |
-
self.current_packages = package_list
|
84 |
-
errors = self.fetch_if_needed(package_list)
|
85 |
-
return self.plot(use_log_scale), errors, gr.update(visible=errors is not None)
|
86 |
-
|
87 |
tracker = DownloadsTracker()
|
88 |
|
89 |
css = """
|
@@ -91,15 +59,13 @@ css = """
|
|
91 |
#textbox_id span {background-color: red}
|
92 |
"""
|
93 |
|
94 |
-
with gr.Blocks(theme=gr.themes.Soft(), css=css) as
|
95 |
packages = gr.Textbox("transformers", label="Package names (comma-separated)")
|
96 |
-
log_scale = gr.Checkbox(label="Use logarithmic scale
|
97 |
error_box = gr.Textbox(label="Errors:", interactive=False, visible=False, elem_id="textbox_id")
|
98 |
render_btn = gr.Button("Render")
|
99 |
plot = gr.Plot()
|
100 |
render_btn.click(tracker.render, inputs=[packages, log_scale], outputs=[plot, error_box, error_box])
|
101 |
log_scale.change(tracker.plot, inputs=[log_scale], outputs=[plot])
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
demo.launch()
|
|
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
5 |
|
|
|
6 |
class DownloadsTracker:
|
7 |
def __init__(self):
|
8 |
self.all_packages = {}
|
|
|
26 |
df["date"] = pd.to_datetime(df["date"])
|
27 |
df = df.sort_values("date")
|
28 |
df["cumulative_downloads"] = df["downloads"].cumsum()
|
|
|
|
|
29 |
self.all_packages[pkg] = df
|
30 |
except:
|
31 |
errors.append(f"Error fetching {pkg}")
|
|
|
33 |
return "\n".join(errors)
|
34 |
return None
|
35 |
|
36 |
+
def render(self, package_list, use_log_scale):
|
37 |
+
package_list = [p.strip() for p in package_list.split(",") if p.strip()]
|
38 |
+
self.current_packages = package_list
|
39 |
+
errors = self.fetch_if_needed(package_list)
|
40 |
+
return self.plot(use_log_scale), errors, gr.update(visible=errors is not None)
|
41 |
+
|
42 |
def plot(self, use_log_scale):
|
43 |
+
fig = px.line(title="Cumulative Package Downloads")
|
|
|
|
|
|
|
|
|
44 |
for pkg in self.current_packages:
|
45 |
if pkg in self.all_packages:
|
46 |
+
df = self.all_packages[pkg]
|
47 |
+
fig.add_scatter(x=df["date"], y=df["cumulative_downloads"], name=pkg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
if use_log_scale:
|
50 |
fig.update_yaxes(type="log")
|
51 |
else:
|
52 |
fig.update_yaxes(type="linear")
|
|
|
53 |
return fig
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
tracker = DownloadsTracker()
|
56 |
|
57 |
css = """
|
|
|
59 |
#textbox_id span {background-color: red}
|
60 |
"""
|
61 |
|
62 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css) as app:
|
63 |
packages = gr.Textbox("transformers", label="Package names (comma-separated)")
|
64 |
+
log_scale = gr.Checkbox(label="Use logarithmic scale", value=False)
|
65 |
error_box = gr.Textbox(label="Errors:", interactive=False, visible=False, elem_id="textbox_id")
|
66 |
render_btn = gr.Button("Render")
|
67 |
plot = gr.Plot()
|
68 |
render_btn.click(tracker.render, inputs=[packages, log_scale], outputs=[plot, error_box, error_box])
|
69 |
log_scale.change(tracker.plot, inputs=[log_scale], outputs=[plot])
|
70 |
|
71 |
+
app.launch()
|
|
|
|