Spaces:
Running

GRADIOBQ / app.py
bqmcp
Dynamic BQ Tables
1a5187a
Raw History Blame Contribute Delete
2.06 kB
import gradio as gr
from AI_QUERY import bq_query
DEFAULT_PROJECT = "project-627b8b21-5a0c-430f-aa1"
DEFAULT_DATASET = "blog_analysis"
def run_bq_query(sql_query, project_id=DEFAULT_PROJECT):
try:
result = bq_query(sql_query, project_id)
if "rows" not in result or "schema" not in result:
return [], [], "Unexpected response from BigQuery."
headers = [field["name"] for field in result["schema"]["fields"]]
rows = [[cell.get("v", None) for cell in row["f"]] for row in result.get("rows", [])]
if not rows:
return [], [], f"No data returned for query: {sql_query}"
return headers, rows, None
except Exception as e:
return [], [], str(e)
black_theme = gr.themes.Soft(
primary_hue="neutral",
secondary_hue="neutral",
neutral_hue="neutral",
).set(
button_primary_background_fill="#000000",
button_primary_background_fill_hover="#333333",
button_primary_text_color="#FFFFFF",
block_title_text_color="#000000",
)
with gr.Blocks(theme=black_theme) as demo:
gr.Markdown("## 🖤 YOUR SQL Runner")
gr.Markdown("Write your SQL query below and click **Run Query** to execute it on Google BigQuery.")
sql_input = gr.Textbox(
label="SQL Query",
lines=8,
value=f"SELECT * FROM `{DEFAULT_PROJECT}.{DEFAULT_DATASET}.INFORMATION_SCHEMA.TABLES` LIMIT 10",
placeholder="Write your BigQuery SQL here..."
)
run_button = gr.Button("▶️ Run Query", variant="primary")
output_table = gr.Dataframe(label="Query Results")
error_box = gr.Textbox(label="Messages / Errors", interactive=False)
def handle_run(sql_query):
headers, rows, error = run_bq_query(sql_query)
if error:
return gr.update(value=None, headers=[]), error
return gr.update(value=rows, headers=headers), ""
run_button.click(
fn=handle_run,
inputs=[sql_input],
outputs=[output_table, error_box]
)
demo.launch(debug=True, share=True, mcp_server=True)