Spaces:
Sleeping
Sleeping
styling
Browse files
main.py
CHANGED
@@ -31,36 +31,49 @@ def query_from_request(query, request: gr.Request):
|
|
31 |
base64query = decoded.base64
|
32 |
else:
|
33 |
decoded = Q.from_base64(base64query)
|
34 |
-
href = format_href(base64query)
|
35 |
-
result = f"""<iframe src="/q/{base64query}" width="90%" height="90%"></iframe>"""
|
36 |
-
return (decoded, href, result)
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
@app.get("/q/{base64query}", response_class=HTMLResponse)
|
43 |
def query_db(base64query: str|None = None):
|
44 |
decoded = Q.from_base64(base64query)
|
45 |
df = decoded.df()
|
46 |
-
html = to_html_datatable(df)
|
47 |
return f"""
|
48 |
<h3>{decoded}</h3>
|
49 |
<div>{html}</div>
|
50 |
"""
|
51 |
|
52 |
-
with gr.Blocks(
|
|
|
|
|
|
|
53 |
with gr.Row():
|
54 |
-
with gr.Column():
|
55 |
header = gr.Markdown("# SQL Editor")
|
56 |
-
sql_code = gr.Code(language="sql", label="SQL Query", interactive=True)
|
57 |
-
copy_button = gr.HTML()
|
58 |
button = gr.Button("run")
|
59 |
-
|
|
|
|
|
60 |
markdown = gr.Markdown("# RESULTS")
|
61 |
results = gr.HTML()
|
62 |
-
button.click(query_from_request, [sql_code], [sql_code,
|
63 |
-
gradio_sql_interface.load(query_from_request, [sql_code], [sql_code,
|
64 |
|
65 |
app = gr.mount_gradio_app(app, gradio_sql_interface, path="/sql")
|
66 |
|
@@ -68,70 +81,4 @@ app = gr.mount_gradio_app(app, gradio_sql_interface, path="/sql")
|
|
68 |
@app.get("/sql")
|
69 |
@app.get("/sql/")
|
70 |
def redirect_to_example():
|
71 |
-
return RedirectResponse("/sql/?q=example")
|
72 |
-
|
73 |
-
# no need to mount/unmount dynamically
|
74 |
-
# just change path and app blocks?
|
75 |
-
# {
|
76 |
-
# "api.routes": [
|
77 |
-
# "...",
|
78 |
-
# "Mount(path='/gradiosql/qq', name='', app=<gradio.routes.App object at 0x71dc7b9d6d10>)"
|
79 |
-
# ]
|
80 |
-
# }
|
81 |
-
# @app.get("/sql/{query}")
|
82 |
-
# def render_query(query: str):
|
83 |
-
# # unmount any previous gradio apps
|
84 |
-
# # for route in app.routes:
|
85 |
-
# # if "gradio" in route.path:
|
86 |
-
# # del route
|
87 |
-
# # global app
|
88 |
-
# # routes = [route for route in app.routes if 'gradio' not in route.path]
|
89 |
-
# # app = Starlette(routes=routes)
|
90 |
-
# with gr.Blocks() as gradio_sql_interface:
|
91 |
-
# sql_code = gr.Code(value=query, language="sql", label="SQL Query", interactive=True)
|
92 |
-
# button = gr.Button("run", link=f"/sql/{sql_code.value}")
|
93 |
-
# markdown = gr.Markdown("# RESULTS")
|
94 |
-
# html = gr.HTML(value=f"""<iframe src="/q/{query}" width="90%" height="90%"></iframe>""")
|
95 |
-
# # button.click(None, [], [], js=f"window.open('/sql/{sql_code.value}', '_top')")
|
96 |
-
|
97 |
-
# gradio_path = f"/gradiosql/{sql_code.value}"
|
98 |
-
# gr.mount_gradio_app(app, gradio_sql_interface, path=gradio_path)
|
99 |
-
|
100 |
-
# return RedirectResponse(gradio_path)
|
101 |
-
|
102 |
-
# del app.routes[index]
|
103 |
-
# @app.get("/redirect")
|
104 |
-
# async def redirect_example():
|
105 |
-
# url = "/q/qqq"
|
106 |
-
# return Response(status_code=302, headers={"Location": url})
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
# # return """hi<hr><iframe src="/" width="80%" height="80%"></iframe>"""
|
111 |
-
|
112 |
-
# @app.get("/qq/{apath}")
|
113 |
-
# async def build_sql_interface(apath: str):
|
114 |
-
# with gr.Blocks() as gradio_sql_interface:
|
115 |
-
# sql_code = gr.Code(value=apath, language="sql", label="SQL Query", interactive=True)
|
116 |
-
# html_code = gr.Code(value=html)
|
117 |
-
# html_html = gr.HTML(value=f"""<iframe src="/q/{apath}" width="80%" height="80%"></iframe>""")
|
118 |
-
# grdf = gr.DataFrame(value=df)
|
119 |
-
# # gradio_sql_interface.queue()
|
120 |
-
|
121 |
-
# gr.mount_gradio_app(app, gradio_sql_interface, path="/gradio/{apath}")
|
122 |
-
# # import time; time.sleep(1)
|
123 |
-
# return RedirectResponse("/gradio/{{apath}}")
|
124 |
-
# async with httpx.AsyncClient() as client:
|
125 |
-
# try:
|
126 |
-
# response = await client.get("/gradio")
|
127 |
-
# response.raise_for_status()
|
128 |
-
# return response
|
129 |
-
# except httpx.HTTPError as e:
|
130 |
-
# return {"errresponse": e}
|
131 |
-
# pass #raise HTTPException(status_code=exc.status_code, detail=exc.msg)
|
132 |
-
# return {"path":path}
|
133 |
-
|
134 |
-
# if __name__ == "__main__":
|
135 |
-
# import uvicorn
|
136 |
-
# uvicorn.run(app, host="0.0.0.0", port=7860)
|
137 |
-
# appenv/.venv/bin/uvicorn main:app --host 0.0.0.0 --port 7860
|
|
|
31 |
base64query = decoded.base64
|
32 |
else:
|
33 |
decoded = Q.from_base64(base64query)
|
|
|
|
|
|
|
34 |
|
35 |
+
_host = request.headers.get("Host")
|
36 |
+
if "huggingface.co/spaces" in _host:
|
37 |
+
# modify URL to access endpoints that aren't available on default app landing page
|
38 |
+
split_url = _host.rsplit("/", maxsplit=2)
|
39 |
+
hf_user, hf_space = split_url[1], split_url[2]
|
40 |
+
host = f"https://{hf_user}-{hf_space}.hf.space"
|
41 |
+
else:
|
42 |
+
host = _host
|
43 |
+
editor_url = f"{host}/sql/?q={base64query}"
|
44 |
+
query_url = f"{host}/q/{base64query}"
|
45 |
+
result = f"""
|
46 |
+
<div id="resultContainer">
|
47 |
+
<iframe src="/q/{base64query}" width="90%" height="90%"></iframe>
|
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="#resultsContainer {height: 75vh;}"
|
64 |
+
) as gradio_sql_interface:
|
65 |
with gr.Row():
|
66 |
+
with gr.Column(scale=1):
|
67 |
header = gr.Markdown("# SQL Editor")
|
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 Editor URL", lines=1)
|
72 |
+
with gr.Column(scale=2):
|
73 |
markdown = gr.Markdown("# RESULTS")
|
74 |
results = gr.HTML()
|
75 |
+
button.click(query_from_request, [sql_code], [sql_code, editor_url, query_url, results])
|
76 |
+
gradio_sql_interface.load(query_from_request, [sql_code], [sql_code, editor_url, query_url, results])
|
77 |
|
78 |
app = gr.mount_gradio_app(app, gradio_sql_interface, path="/sql")
|
79 |
|
|
|
81 |
@app.get("/sql")
|
82 |
@app.get("/sql/")
|
83 |
def redirect_to_example():
|
84 |
+
return RedirectResponse("/sql/?q=example")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|