azizalto commited on
Commit
b8b9cbf
·
verified ·
1 Parent(s): 972d14f

deploy at 2024-08-12 17:33:06.979023

Browse files
Files changed (6) hide show
  1. Dockerfile +10 -0
  2. README.md +26 -10
  3. app.py +32 -0
  4. code_runner.py +14 -0
  5. config.ini +5 -0
  6. requirements.txt +2 -0
Dockerfile ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+ WORKDIR /code
3
+ COPY --link --chown=1000 . .
4
+ RUN mkdir -p /tmp/cache/
5
+ RUN chmod a+rwx -R /tmp/cache/
6
+ ENV HF_HUB_CACHE=HF_HOME
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ ENV PYTHONUNBUFFERED=1 PORT=7860
10
+ CMD ["python", "main.py"]
README.md CHANGED
@@ -1,10 +1,26 @@
1
- ---
2
- title: Fh Pyconsole
3
- emoji: 🚀
4
- colorFrom: pink
5
- colorTo: blue
6
- sdk: docker
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PyConsole
2
+
3
+ Turn browser into a console for Python using [FastHTML](https://github.com/answerdotai/fasthtml) (in 34 lines).
4
+ # Usage
5
+
6
+ ```shell
7
+ pip install python-fasthtml
8
+ git clone https://github.com/iamaziz/fh_pyconsole.git
9
+ cd fh_pyconsole
10
+ ```
11
+
12
+ ```python
13
+ python app.py
14
+ ```
15
+
16
+ Open browser and navigate to `http://localhost:5001`
17
+
18
+ # Demo
19
+
20
+ > `<TODO> add screenshare video recording`
21
+
22
+ <br>
23
+
24
+ ## Disclaimer ⚠️
25
+
26
+ > Under the hood, `exec()` is used to execute Python code.
app.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()
code_runner.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ from io import StringIO
3
+
4
+
5
+ def execute_code(code: str):
6
+ try:
7
+ old_stdout = sys.stdout
8
+ redirected_output = sys.stdout = StringIO()
9
+ exec(code, globals())
10
+ sys.stdout = old_stdout
11
+ return redirected_output.getvalue()
12
+ except Exception as e:
13
+ return str(e)
14
+
config.ini ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ [DEFAULT]
2
+ dataset_id = space-backup
3
+ db_dir = data
4
+ private_backup = True
5
+
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ python-fasthtml
2
+ fasthtml-hf