Spaces:
Sleeping
Sleeping
deploy at 2024-08-12 17:38:13.082420
Browse files
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 🐍
|
|
4 |
colorFrom: green
|
5 |
colorTo: green
|
6 |
sdk: docker
|
7 |
-
app_file:
|
8 |
pinned: false
|
9 |
---
|
10 |
|
@@ -35,4 +35,9 @@ Open browser and navigate to `http://localhost:5001`
|
|
35 |
|
36 |
## Disclaimer ⚠️
|
37 |
|
38 |
-
> Under the hood, `exec()` is used to execute Python code.
|
|
|
|
|
|
|
|
|
|
|
|
4 |
colorFrom: green
|
5 |
colorTo: green
|
6 |
sdk: docker
|
7 |
+
app_file: main.py
|
8 |
pinned: false
|
9 |
---
|
10 |
|
|
|
35 |
|
36 |
## Disclaimer ⚠️
|
37 |
|
38 |
+
> Under the hood, `exec()` is used to execute Python code.
|
39 |
+
|
40 |
+
|
41 |
+
## Deployment to HF Space
|
42 |
+
|
43 |
+
- Using [fasthtml-hf](https://github.com/AnswerDotAI/fasthtml-hf?tab=readme-ov-file#quickstart)
|
main.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fasthtml.common import (
|
2 |
+
Main, Div, Form, Input, Code, Pre, fast_app, serve, picolink, MarkdownJS, HighlightJS
|
3 |
+
)
|
4 |
+
from fasthtml_hf import setup_hf_backup # to deploy to HF spaces
|
5 |
+
|
6 |
+
from code_runner import execute_code as execute
|
7 |
+
|
8 |
+
app, rt = fast_app(hdrs=[picolink, MarkdownJS(), HighlightJS()])
|
9 |
+
log = []
|
10 |
+
|
11 |
+
|
12 |
+
@app.get("/")
|
13 |
+
def pyconsole():
|
14 |
+
return Main(
|
15 |
+
Div(id="output"),
|
16 |
+
Form(
|
17 |
+
Input(name="code", placeholder=">>> Type Python code here (and hit enter)"),
|
18 |
+
hx_post="/run", hx_target="#output", hx_trigger="submit", id="input_form",
|
19 |
+
),
|
20 |
+
cls="container",
|
21 |
+
hx_swap="innerHTML show:window:bottom", # automatically scroll to the bottom of the page
|
22 |
+
)
|
23 |
+
|
24 |
+
|
25 |
+
@app.post("/run")
|
26 |
+
def run_code(data: dict):
|
27 |
+
log.append((data["code"], execute(data["code"])))
|
28 |
+
return Div(*[Div(Pre(Code(code, klass="python")), Div(output, cls="marked"), Div("---", cls="marked")) for code, output in log])
|
29 |
+
|
30 |
+
|
31 |
+
setup_hf_backup(app)
|
32 |
+
serve()
|