Spaces:
Sleeping
Sleeping
itoptions styling
Browse files
main.py
CHANGED
|
@@ -10,12 +10,44 @@ import gradio as gr
|
|
| 10 |
import pandas as pd
|
| 11 |
from fastapi import FastAPI
|
| 12 |
from fastapi.responses import HTMLResponse, RedirectResponse
|
| 13 |
-
from itables import to_html_datatable
|
| 14 |
|
| 15 |
from sql import Q
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
app = FastAPI()
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def query_from_request(query, request: gr.Request):
|
| 20 |
"""Process query from input block or from initial request.
|
| 21 |
|
|
@@ -27,7 +59,7 @@ def query_from_request(query, request: gr.Request):
|
|
| 27 |
else:
|
| 28 |
base64query = Q(query).base64
|
| 29 |
if base64query in (None, "example"):
|
| 30 |
-
decoded = Q(
|
| 31 |
base64query = decoded.base64
|
| 32 |
else:
|
| 33 |
decoded = Q.from_base64(base64query)
|
|
@@ -48,19 +80,9 @@ def query_from_request(query, request: gr.Request):
|
|
| 48 |
</div>"""
|
| 49 |
return (decoded, editor_url, query_url, result)
|
| 50 |
|
| 51 |
-
@app.get("/q/{base64query}", response_class=HTMLResponse)
|
| 52 |
-
def query_db(base64query: str|None = None):
|
| 53 |
-
decoded = Q.from_base64(base64query)
|
| 54 |
-
df = decoded.df()
|
| 55 |
-
html = to_html_datatable(df, classes="display compact cell-border")
|
| 56 |
-
return f"""
|
| 57 |
-
<h3>{decoded}</h3>
|
| 58 |
-
<div>{html}</div>
|
| 59 |
-
"""
|
| 60 |
-
|
| 61 |
with gr.Blocks(
|
| 62 |
title="Gradio DuckDB Editor",
|
| 63 |
-
css="#
|
| 64 |
) as gradio_sql_interface:
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column(scale=1):
|
|
@@ -68,7 +90,7 @@ with gr.Blocks(
|
|
| 68 |
sql_code = gr.Code(language="sql", label="SQL Query", lines=32, interactive=True)
|
| 69 |
button = gr.Button("run")
|
| 70 |
editor_url = gr.Code(label="Share Editor URL", lines=1)
|
| 71 |
-
query_url = gr.Code(label="Share
|
| 72 |
with gr.Column(scale=2):
|
| 73 |
markdown = gr.Markdown("# RESULTS")
|
| 74 |
results = gr.HTML()
|
|
|
|
| 10 |
import pandas as pd
|
| 11 |
from fastapi import FastAPI
|
| 12 |
from fastapi.responses import HTMLResponse, RedirectResponse
|
| 13 |
+
from itables import options as itoptions, to_html_datatable
|
| 14 |
|
| 15 |
from sql import Q
|
| 16 |
|
| 17 |
+
itoptions.classes = "display compact cell-border"
|
| 18 |
+
itoptions.column_filters = "footer"
|
| 19 |
+
|
| 20 |
+
EXAMPLE1 = """
|
| 21 |
+
SELECT
|
| 22 |
+
Symbol,
|
| 23 |
+
Number,
|
| 24 |
+
Mass,
|
| 25 |
+
Abundance
|
| 26 |
+
FROM 'https://raw.githubusercontent.com/ekwan/cctk/master/cctk/data/isotopes.csv'
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
EXAMPLE2 = """
|
| 30 |
+
SELECT
|
| 31 |
+
42 AS answer,
|
| 32 |
+
'Life, Universe & Everything' AS question
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
app = FastAPI()
|
| 36 |
|
| 37 |
+
@app.get("/q/{base64query}", response_class=HTMLResponse)
|
| 38 |
+
def query_db(base64query: str|None = None):
|
| 39 |
+
"""Endpoint for running b64-encoded SQL queries."""
|
| 40 |
+
decoded = Q.from_base64(base64query)
|
| 41 |
+
df = decoded.df()
|
| 42 |
+
html = to_html_datatable(df)
|
| 43 |
+
return f"""
|
| 44 |
+
<head>
|
| 45 |
+
<link rel="stylesheet" href="https://cdn.datatables.net/2.0.5/css/dataTables.dataTables.min.css" />
|
| 46 |
+
</head>
|
| 47 |
+
<div>{html}</div>
|
| 48 |
+
<hr><pre>{decoded}</pre>
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
def query_from_request(query, request: gr.Request):
|
| 52 |
"""Process query from input block or from initial request.
|
| 53 |
|
|
|
|
| 59 |
else:
|
| 60 |
base64query = Q(query).base64
|
| 61 |
if base64query in (None, "example"):
|
| 62 |
+
decoded = Q(EXAMPLE2)
|
| 63 |
base64query = decoded.base64
|
| 64 |
else:
|
| 65 |
decoded = Q.from_base64(base64query)
|
|
|
|
| 80 |
</div>"""
|
| 81 |
return (decoded, editor_url, query_url, result)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
with gr.Blocks(
|
| 84 |
title="Gradio DuckDB Editor",
|
| 85 |
+
css="#resultContainer {height: 75vh;}"
|
| 86 |
) as gradio_sql_interface:
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column(scale=1):
|
|
|
|
| 90 |
sql_code = gr.Code(language="sql", label="SQL Query", lines=32, interactive=True)
|
| 91 |
button = gr.Button("run")
|
| 92 |
editor_url = gr.Code(label="Share Editor URL", lines=1)
|
| 93 |
+
query_url = gr.Code(label="Share Query Results URL", lines=1)
|
| 94 |
with gr.Column(scale=2):
|
| 95 |
markdown = gr.Markdown("# RESULTS")
|
| 96 |
results = gr.HTML()
|