Spaces:
Sleeping
Sleeping
Update tools/plot_generator.py
Browse files- tools/plot_generator.py +12 -9
tools/plot_generator.py
CHANGED
|
@@ -1,25 +1,28 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import plotly.graph_objects as go
|
| 3 |
|
| 4 |
-
def
|
| 5 |
"""
|
| 6 |
-
|
| 7 |
-
|
| 8 |
"""
|
| 9 |
df = pd.read_csv(file_path)
|
| 10 |
|
| 11 |
-
if date_col not in df.columns or
|
| 12 |
-
return f"❌ '{date_col}' or '
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
fig = go.Figure(
|
| 15 |
go.Scatter(
|
| 16 |
x=df[date_col],
|
| 17 |
-
y=df[
|
| 18 |
mode="lines+markers",
|
| 19 |
line=dict(width=2),
|
| 20 |
)
|
| 21 |
)
|
| 22 |
-
fig.update_layout(title="
|
| 23 |
-
fig.write_image("
|
| 24 |
-
|
| 25 |
return fig
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import plotly.graph_objects as go
|
| 3 |
|
| 4 |
+
def plot_metric_tool(file_path: str, date_col: str, value_col: str):
|
| 5 |
"""
|
| 6 |
+
Generic interactive line chart for any numeric metric.
|
| 7 |
+
Returns a Plotly Figure or error message string.
|
| 8 |
"""
|
| 9 |
df = pd.read_csv(file_path)
|
| 10 |
|
| 11 |
+
if date_col not in df.columns or value_col not in df.columns:
|
| 12 |
+
return f"❌ '{date_col}' or '{value_col}' column missing."
|
| 13 |
+
|
| 14 |
+
df[date_col] = pd.to_datetime(df[date_col], errors="coerce")
|
| 15 |
+
if df[date_col].isna().all():
|
| 16 |
+
return f"❌ '{date_col}' could not be parsed as dates."
|
| 17 |
|
| 18 |
fig = go.Figure(
|
| 19 |
go.Scatter(
|
| 20 |
x=df[date_col],
|
| 21 |
+
y=df[value_col],
|
| 22 |
mode="lines+markers",
|
| 23 |
line=dict(width=2),
|
| 24 |
)
|
| 25 |
)
|
| 26 |
+
fig.update_layout(title=f"{value_col} Trend", template="plotly_dark")
|
| 27 |
+
fig.write_image("metric_plot.png")
|
|
|
|
| 28 |
return fig
|