Jeremy Live
commited on
Commit
路
8bb4446
1
Parent(s):
8717362
mm1
Browse files- app.py +38 -9
- requirements.txt +1 -0
app.py
CHANGED
@@ -367,25 +367,53 @@ def execute_sql_query(query, db_connection):
|
|
367 |
result = connection.execute(sa_text(query))
|
368 |
else:
|
369 |
result = connection.execute(query)
|
|
|
|
|
|
|
370 |
rows = result.fetchall()
|
371 |
-
|
372 |
# Convertir los resultados a un formato legible
|
373 |
if not rows:
|
374 |
return "La consulta no devolvi贸 resultados"
|
375 |
-
|
376 |
# Si es un solo resultado, devolverlo directamente
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
|
|
|
|
|
|
381 |
try:
|
382 |
import pandas as pd
|
383 |
-
|
384 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
385 |
except ImportError:
|
386 |
# Si pandas no est谩 disponible, usar formato simple
|
387 |
return "\n".join([str(row) for row in rows])
|
388 |
-
|
389 |
except Exception as e:
|
390 |
return f"Error ejecutando la consulta: {str(e)}"
|
391 |
|
@@ -836,6 +864,7 @@ def create_ui():
|
|
836 |
)
|
837 |
|
838 |
# Chart display area (interactive Plotly figure)
|
|
|
839 |
chart_display = gr.Plot(
|
840 |
label="馃搳 Visualizaci贸n",
|
841 |
)
|
|
|
367 |
result = connection.execute(sa_text(query))
|
368 |
else:
|
369 |
result = connection.execute(query)
|
370 |
+
|
371 |
+
# Fetch data and column names
|
372 |
+
columns = list(result.keys()) if hasattr(result, "keys") else []
|
373 |
rows = result.fetchall()
|
374 |
+
|
375 |
# Convertir los resultados a un formato legible
|
376 |
if not rows:
|
377 |
return "La consulta no devolvi贸 resultados"
|
378 |
+
|
379 |
# Si es un solo resultado, devolverlo directamente
|
380 |
+
try:
|
381 |
+
if len(rows) == 1 and len(rows[0]) == 1:
|
382 |
+
return str(rows[0][0])
|
383 |
+
except Exception:
|
384 |
+
pass
|
385 |
+
|
386 |
+
# Si hay m煤ltiples filas, formatear como tabla Markdown
|
387 |
try:
|
388 |
import pandas as pd
|
389 |
+
|
390 |
+
# Convert SQLAlchemy Row objects to list of dicts using column names
|
391 |
+
if columns:
|
392 |
+
data = [
|
393 |
+
{col: val for col, val in zip(columns, tuple(row))}
|
394 |
+
for row in rows
|
395 |
+
]
|
396 |
+
df = pd.DataFrame(data)
|
397 |
+
else:
|
398 |
+
# Fallback: let pandas infer columns
|
399 |
+
df = pd.DataFrame(rows)
|
400 |
+
|
401 |
+
# Prefer Markdown output for downstream chart parsing
|
402 |
+
try:
|
403 |
+
return df.to_markdown(index=False)
|
404 |
+
except Exception:
|
405 |
+
# If optional dependency 'tabulate' is missing, build a simple Markdown table
|
406 |
+
headers = list(map(str, df.columns))
|
407 |
+
header_line = "| " + " | ".join(headers) + " |"
|
408 |
+
sep_line = "| " + " | ".join(["---"] * len(headers)) + " |"
|
409 |
+
body_lines = []
|
410 |
+
for _, r in df.iterrows():
|
411 |
+
body_lines.append("| " + " | ".join(map(lambda v: str(v), r.values)) + " |")
|
412 |
+
return "\n".join([header_line, sep_line, *body_lines])
|
413 |
except ImportError:
|
414 |
# Si pandas no est谩 disponible, usar formato simple
|
415 |
return "\n".join([str(row) for row in rows])
|
416 |
+
|
417 |
except Exception as e:
|
418 |
return f"Error ejecutando la consulta: {str(e)}"
|
419 |
|
|
|
864 |
)
|
865 |
|
866 |
# Chart display area (interactive Plotly figure)
|
867 |
+
# In Gradio 5, gr.Plot accepts a plotly.graph_objects.Figure
|
868 |
chart_display = gr.Plot(
|
869 |
label="馃搳 Visualizaci贸n",
|
870 |
)
|
requirements.txt
CHANGED
@@ -14,3 +14,4 @@ numpy==1.26.4
|
|
14 |
python-multipart>=0.0.18 # Required by gradio
|
15 |
plotly==5.18.0 # For interactive charts
|
16 |
kaleido==0.2.1 # For saving plotly charts as images
|
|
|
|
14 |
python-multipart>=0.0.18 # Required by gradio
|
15 |
plotly==5.18.0 # For interactive charts
|
16 |
kaleido==0.2.1 # For saving plotly charts as images
|
17 |
+
tabulate>=0.9.0 # Enables DataFrame.to_markdown used for chart parsing
|