Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ HEADERS = {
|
|
| 24 |
}
|
| 25 |
|
| 26 |
# =====================================================
|
| 27 |
-
# DASHBOARD PRESETS
|
| 28 |
# =====================================================
|
| 29 |
DASHBOARD_PRESETS = {
|
| 30 |
"Total Visits by Industry": ("primary_industry", "total_visits", "sum"),
|
|
@@ -41,7 +41,7 @@ DASHBOARD_PRESETS = {
|
|
| 41 |
}
|
| 42 |
|
| 43 |
# =====================================================
|
| 44 |
-
# TREND PRESETS
|
| 45 |
# =====================================================
|
| 46 |
TREND_PRESETS = {
|
| 47 |
"Total Visits Trend": ("total_visits", "sum", None),
|
|
@@ -64,7 +64,7 @@ def authenticate(pwd):
|
|
| 64 |
return True
|
| 65 |
|
| 66 |
# =====================================================
|
| 67 |
-
# DATA FETCH (WORKING
|
| 68 |
# =====================================================
|
| 69 |
def fetch_company_data(start_date, end_date):
|
| 70 |
page, rows = 1, []
|
|
@@ -142,11 +142,21 @@ def fetch_company_data(start_date, end_date):
|
|
| 142 |
return df
|
| 143 |
|
| 144 |
# =====================================================
|
| 145 |
-
#
|
| 146 |
# =====================================================
|
| 147 |
-
def
|
| 148 |
df = fetch_company_data(start_date, end_date)
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
# =====================================================
|
| 152 |
# KPI CARDS
|
|
@@ -214,15 +224,25 @@ with gr.Blocks(title="Leadfeeder Company-Level Analytics") as demo:
|
|
| 214 |
|
| 215 |
with gr.Tabs():
|
| 216 |
|
|
|
|
| 217 |
with gr.Tab("π Data"):
|
| 218 |
start = gr.Textbox(value=(date.today() - timedelta(days=30)).isoformat())
|
| 219 |
end = gr.Textbox(value=date.today().isoformat())
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
[start, end],
|
| 223 |
-
[df_state, status]
|
| 224 |
)
|
| 225 |
|
|
|
|
|
|
|
|
|
|
| 226 |
with gr.Tab("π Dashboard"):
|
| 227 |
with gr.Row():
|
| 228 |
k1 = gr.Number(label="Total Companies")
|
|
@@ -240,6 +260,7 @@ with gr.Blocks(title="Leadfeeder Company-Level Analytics") as demo:
|
|
| 240 |
|
| 241 |
gr.Button("Build View").click(build_dashboard, [df_state, preset, top_n], chart)
|
| 242 |
|
|
|
|
| 243 |
with gr.Tab("π Trends"):
|
| 244 |
trend_view = gr.Dropdown(choices=list(TREND_PRESETS.keys()))
|
| 245 |
grain = gr.Radio(["Daily", "Weekly", "Monthly"], value="Daily")
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
# =====================================================
|
| 27 |
+
# DASHBOARD PRESETS
|
| 28 |
# =====================================================
|
| 29 |
DASHBOARD_PRESETS = {
|
| 30 |
"Total Visits by Industry": ("primary_industry", "total_visits", "sum"),
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
# =====================================================
|
| 44 |
+
# TREND PRESETS
|
| 45 |
# =====================================================
|
| 46 |
TREND_PRESETS = {
|
| 47 |
"Total Visits Trend": ("total_visits", "sum", None),
|
|
|
|
| 64 |
return True
|
| 65 |
|
| 66 |
# =====================================================
|
| 67 |
+
# DATA FETCH (WORKING)
|
| 68 |
# =====================================================
|
| 69 |
def fetch_company_data(start_date, end_date):
|
| 70 |
page, rows = 1, []
|
|
|
|
| 142 |
return df
|
| 143 |
|
| 144 |
# =====================================================
|
| 145 |
+
# LOAD WRAPPER (STATE SAFE)
|
| 146 |
# =====================================================
|
| 147 |
+
def load_data_with_preview(start_date, end_date):
|
| 148 |
df = fetch_company_data(start_date, end_date)
|
| 149 |
+
preview = df.head(500)
|
| 150 |
+
status = f"β
Loaded **{len(df):,} companies** (showing first 500 rows)"
|
| 151 |
+
return df, preview, status
|
| 152 |
+
|
| 153 |
+
# =====================================================
|
| 154 |
+
# EXCEL EXPORT
|
| 155 |
+
# =====================================================
|
| 156 |
+
def export_excel(df):
|
| 157 |
+
path = "/tmp/leadfeeder_company_data.xlsx"
|
| 158 |
+
df.to_excel(path, index=False)
|
| 159 |
+
return path
|
| 160 |
|
| 161 |
# =====================================================
|
| 162 |
# KPI CARDS
|
|
|
|
| 224 |
|
| 225 |
with gr.Tabs():
|
| 226 |
|
| 227 |
+
# ================= DATA =================
|
| 228 |
with gr.Tab("π Data"):
|
| 229 |
start = gr.Textbox(value=(date.today() - timedelta(days=30)).isoformat())
|
| 230 |
end = gr.Textbox(value=date.today().isoformat())
|
| 231 |
+
|
| 232 |
+
load_btn = gr.Button("Load Data")
|
| 233 |
+
preview_table = gr.Dataframe(interactive=True)
|
| 234 |
+
download_btn = gr.Button("β¬οΈ Download Excel")
|
| 235 |
+
download_file = gr.File()
|
| 236 |
+
|
| 237 |
+
load_btn.click(
|
| 238 |
+
load_data_with_preview,
|
| 239 |
[start, end],
|
| 240 |
+
[df_state, preview_table, status]
|
| 241 |
)
|
| 242 |
|
| 243 |
+
download_btn.click(export_excel, df_state, download_file)
|
| 244 |
+
|
| 245 |
+
# ================= DASHBOARD =================
|
| 246 |
with gr.Tab("π Dashboard"):
|
| 247 |
with gr.Row():
|
| 248 |
k1 = gr.Number(label="Total Companies")
|
|
|
|
| 260 |
|
| 261 |
gr.Button("Build View").click(build_dashboard, [df_state, preset, top_n], chart)
|
| 262 |
|
| 263 |
+
# ================= TRENDS =================
|
| 264 |
with gr.Tab("π Trends"):
|
| 265 |
trend_view = gr.Dropdown(choices=list(TREND_PRESETS.keys()))
|
| 266 |
grain = gr.Radio(["Daily", "Weekly", "Monthly"], value="Daily")
|