Levin-Aleksey commited on
Commit
2d603d0
·
1 Parent(s): cd080af
Files changed (2) hide show
  1. __pycache__/app.cpython-312.pyc +0 -0
  2. app.py +62 -30
__pycache__/app.cpython-312.pyc CHANGED
Binary files a/__pycache__/app.cpython-312.pyc and b/__pycache__/app.cpython-312.pyc differ
 
app.py CHANGED
@@ -7,6 +7,46 @@ import os
7
  st.set_page_config(page_title="Crypto Dash", layout="wide")
8
  IS_HF_SPACE = bool(os.getenv("SPACE_ID") or os.getenv("SPACE_REPO_NAME"))
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def load_report_data() -> dict:
11
  """Load report data from data.json next to app.py.
12
 
@@ -52,40 +92,32 @@ c3.metric("New Wallets", f"{data['new_wallets_amount']:,}")
52
  # График когорт
53
  st.subheader("User Activity by Cohort Day")
54
  cohorts_raw = data.get("users_by_cohorts", [])
55
- df_cohorts = pd.DataFrame(cohorts_raw)
56
 
57
- required_cols = {"cohort_day", "user_count"}
58
  if df_cohorts.empty:
59
  st.warning("Нет данных для графика: users_by_cohorts пустой.")
60
- elif not required_cols.issubset(df_cohorts.columns):
61
- st.error("В users_by_cohorts отсутствуют обязательные поля: cohort_day и/или user_count.")
62
  else:
63
- df_cohorts["cohort_day"] = pd.to_numeric(df_cohorts["cohort_day"], errors="coerce")
64
- df_cohorts["user_count"] = pd.to_numeric(df_cohorts["user_count"], errors="coerce")
65
- df_cohorts = df_cohorts.dropna(subset=["cohort_day", "user_count"]).sort_values("cohort_day")
66
-
67
- if df_cohorts.empty:
68
- st.warning("После очистки данных не осталось валидных точек для графика.")
69
- else:
70
- chart_col, debug_col = st.columns([4, 1])
71
-
72
- with chart_col:
73
- try:
74
- fig = px.area(df_cohorts, x="cohort_day", y="user_count", template="plotly")
75
- st.plotly_chart(fig, use_container_width=True)
76
- except Exception as exc:
77
- st.warning(f"Plotly не отрисован ({exc}). Показываю fallback-график Streamlit.")
78
- st.area_chart(df_cohorts.set_index("cohort_day")["user_count"])
79
-
80
- if IS_HF_SPACE:
81
- st.caption("HF fallback chart")
82
- st.area_chart(df_cohorts.set_index("cohort_day")["user_count"])
83
-
84
- with debug_col:
85
- st.caption("Chart debug")
86
- st.metric("Rows", len(df_cohorts))
87
- st.metric("Min day", int(df_cohorts["cohort_day"].min()))
88
- st.metric("Max day", int(df_cohorts["cohort_day"].max()))
89
 
90
  # Проверка данных внизу
91
  with st.expander("Raw Data"):
 
7
  st.set_page_config(page_title="Crypto Dash", layout="wide")
8
  IS_HF_SPACE = bool(os.getenv("SPACE_ID") or os.getenv("SPACE_REPO_NAME"))
9
 
10
+
11
+ def normalize_cohorts(raw_cohorts) -> pd.DataFrame:
12
+ """Return a normalized DataFrame with columns: cohort_day, user_count."""
13
+ if raw_cohorts is None:
14
+ return pd.DataFrame(columns=["cohort_day", "user_count"])
15
+
16
+ if isinstance(raw_cohorts, dict):
17
+ rows = []
18
+ for day, count in raw_cohorts.items():
19
+ rows.append({"cohort_day": day, "user_count": count})
20
+ df = pd.DataFrame(rows)
21
+ else:
22
+ df = pd.DataFrame(raw_cohorts)
23
+
24
+ if df.empty:
25
+ return pd.DataFrame(columns=["cohort_day", "user_count"])
26
+
27
+ # Some payloads can use alternative field names.
28
+ rename_map = {
29
+ "day": "cohort_day",
30
+ "cohort": "cohort_day",
31
+ "count": "user_count",
32
+ "users": "user_count",
33
+ "tx_count": "user_count",
34
+ }
35
+ df = df.rename(columns=rename_map)
36
+
37
+ required_cols = {"cohort_day", "user_count"}
38
+ if not required_cols.issubset(df.columns):
39
+ return pd.DataFrame(columns=["cohort_day", "user_count"])
40
+
41
+ df["cohort_day"] = pd.to_numeric(df["cohort_day"], errors="coerce")
42
+ df["user_count"] = pd.to_numeric(df["user_count"], errors="coerce")
43
+ df = df.dropna(subset=["cohort_day", "user_count"]).sort_values("cohort_day")
44
+
45
+ if df.empty:
46
+ return pd.DataFrame(columns=["cohort_day", "user_count"])
47
+
48
+ return df[["cohort_day", "user_count"]]
49
+
50
  def load_report_data() -> dict:
51
  """Load report data from data.json next to app.py.
52
 
 
92
  # График когорт
93
  st.subheader("User Activity by Cohort Day")
94
  cohorts_raw = data.get("users_by_cohorts", [])
95
+ df_cohorts = normalize_cohorts(cohorts_raw)
96
 
 
97
  if df_cohorts.empty:
98
  st.warning("Нет данных для графика: users_by_cohorts пустой.")
 
 
99
  else:
100
+ chart_col, debug_col = st.columns([4, 1])
101
+
102
+ with chart_col:
103
+ st.caption("Native chart (always on)")
104
+ st.area_chart(df_cohorts.set_index("cohort_day")["user_count"], use_container_width=True)
105
+
106
+ try:
107
+ st.caption("Plotly chart")
108
+ fig = px.area(df_cohorts, x="cohort_day", y="user_count", template="plotly")
109
+ st.plotly_chart(fig, use_container_width=True)
110
+ except Exception as exc:
111
+ st.warning(f"Plotly не отрисован: {exc}")
112
+
113
+ if IS_HF_SPACE:
114
+ st.caption("HF mode: fallback enabled")
115
+
116
+ with debug_col:
117
+ st.caption("Chart debug")
118
+ st.metric("Rows", len(df_cohorts))
119
+ st.metric("Min day", int(df_cohorts["cohort_day"].min()))
120
+ st.metric("Max day", int(df_cohorts["cohort_day"].max()))
 
 
 
 
 
121
 
122
  # Проверка данных внизу
123
  with st.expander("Raw Data"):