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
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
CSV_PATH = Path("metrics.csv")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def load_data() -> pd.DataFrame:
|
| 13 |
+
df = pd.read_csv(CSV_PATH)
|
| 14 |
+
# Normalize/parse columns
|
| 15 |
+
if "date" not in df.columns:
|
| 16 |
+
raise ValueError("Expected a 'date' column in metrics.csv")
|
| 17 |
+
df["date"] = pd.to_datetime(df["date"], errors="coerce")
|
| 18 |
+
df = df.sort_values("date").reset_index(drop=True)
|
| 19 |
+
# Ensure numeric columns are numeric
|
| 20 |
+
for c in df.columns:
|
| 21 |
+
if c == "date":
|
| 22 |
+
continue
|
| 23 |
+
df[c] = pd.to_numeric(df[c], errors="coerce")
|
| 24 |
+
return df
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def line_figure(df: pd.DataFrame, cols: list[str], title: str, yaxis_title: str = "") -> go.Figure:
|
| 28 |
+
if not cols:
|
| 29 |
+
# Empty placeholder so the UI doesn't error
|
| 30 |
+
fig = go.Figure()
|
| 31 |
+
fig.update_layout(title=f"{title} (no series selected)")
|
| 32 |
+
return fig
|
| 33 |
+
fig = px.line(
|
| 34 |
+
df,
|
| 35 |
+
x="date",
|
| 36 |
+
y=cols,
|
| 37 |
+
markers=True,
|
| 38 |
+
title=title,
|
| 39 |
+
)
|
| 40 |
+
# Improve layout for time series
|
| 41 |
+
fig.update_layout(
|
| 42 |
+
legend_title_text="Series",
|
| 43 |
+
xaxis_title="Date",
|
| 44 |
+
yaxis_title=yaxis_title,
|
| 45 |
+
hovermode="x unified",
|
| 46 |
+
margin={"l": 50, "r": 20, "t": 50, "b": 40},
|
| 47 |
+
)
|
| 48 |
+
return fig
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
TAB_SPEC = {
|
| 52 |
+
"Docstrings": [
|
| 53 |
+
"docstring coverage",
|
| 54 |
+
"docstring missing",
|
| 55 |
+
],
|
| 56 |
+
"Size (Lines/Statements/Expressions/Parameters)": [
|
| 57 |
+
"lines mean",
|
| 58 |
+
"lines max",
|
| 59 |
+
"lines 90th-percentile",
|
| 60 |
+
"statements mean",
|
| 61 |
+
"statements max",
|
| 62 |
+
"statements 90th-percentile",
|
| 63 |
+
"expressions mean",
|
| 64 |
+
"expressions max",
|
| 65 |
+
"expressions 90th-percentile",
|
| 66 |
+
"parameters mean",
|
| 67 |
+
"parameters max",
|
| 68 |
+
"parameters 90th-percentile",
|
| 69 |
+
],
|
| 70 |
+
"Complexity": [
|
| 71 |
+
"cyclomatic_complexity mean",
|
| 72 |
+
"cyclomatic_complexity max",
|
| 73 |
+
"cyclomatic_complexity 90th-percentile",
|
| 74 |
+
],
|
| 75 |
+
"Typing": [
|
| 76 |
+
"type_coverage mean",
|
| 77 |
+
"type_coverage min",
|
| 78 |
+
"type_coverage 50th-percentile",
|
| 79 |
+
],
|
| 80 |
+
"Duplication": [
|
| 81 |
+
"duplication.score mean",
|
| 82 |
+
"duplication.score max",
|
| 83 |
+
"duplication.score 90th-percentile",
|
| 84 |
+
"duplication.score 50th-percentile",
|
| 85 |
+
"duplication.duplicated-lines total",
|
| 86 |
+
],
|
| 87 |
+
"TODOs": [
|
| 88 |
+
"todo_comments total",
|
| 89 |
+
],
|
| 90 |
+
"CLOC (Repository scope)": [
|
| 91 |
+
"files",
|
| 92 |
+
"lines blank",
|
| 93 |
+
"lines comment",
|
| 94 |
+
"lines code",
|
| 95 |
+
],
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
Y_LABELS = {
|
| 99 |
+
"Docstrings": "value",
|
| 100 |
+
"Size (Lines/Statements/Expressions/Parameters)": "count",
|
| 101 |
+
"Complexity": "complexity",
|
| 102 |
+
"Typing": "fraction / coverage",
|
| 103 |
+
"Duplication": "score / lines",
|
| 104 |
+
"TODOs": "count",
|
| 105 |
+
"CLOC (Repository scope)": "lines / files",
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
DF = load_data()
|
| 109 |
+
|
| 110 |
+
with gr.Blocks(title="Code Metrics – Time Series", fill_height=True) as demo:
|
| 111 |
+
gr.Markdown(
|
| 112 |
+
"## Code Metrics Over Time\n"
|
| 113 |
+
f"Loaded **{CSV_PATH}** with {len(DF)} rows spanning "
|
| 114 |
+
f"{DF['date'].min().date()} → {DF['date'].max().date()}.\n\n"
|
| 115 |
+
"Use each tab to pick the series you want to plot."
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
with gr.Tabs():
|
| 119 |
+
tab_controls = []
|
| 120 |
+
tab_plots = []
|
| 121 |
+
|
| 122 |
+
for tab_name, series in TAB_SPEC.items():
|
| 123 |
+
available = [s for s in series if s in DF.columns] # guard against missing cols
|
| 124 |
+
with gr.Tab(tab_name):
|
| 125 |
+
with gr.Row():
|
| 126 |
+
sel = gr.CheckboxGroup(
|
| 127 |
+
choices=available,
|
| 128 |
+
value=available,
|
| 129 |
+
label="Series",
|
| 130 |
+
)
|
| 131 |
+
plot = gr.Plot(
|
| 132 |
+
value=line_figure(DF, available, tab_name, Y_LABELS.get(tab_name, "")),
|
| 133 |
+
show_label=False,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
sel.change(
|
| 137 |
+
fn=lambda cols, t=tab_name: line_figure(DF, cols, t, Y_LABELS.get(t, "")),
|
| 138 |
+
inputs=sel,
|
| 139 |
+
outputs=plot,
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
tab_controls.append(sel)
|
| 143 |
+
tab_plots.append(plot)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
if __name__ == "__main__":
|
| 147 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
plotly
|