-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
643 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,6 @@ def dark_media( | |
|
||
# %% ../nbs/api/03_js.ipynb | ||
marked_imp = """import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js"; | ||
import { proc_htmx } from "https://cdn.jsdelivr.net/gh/answerdotai/[email protected]/fasthtml.js"; | ||
""" | ||
npmcdn = 'https://cdn.jsdelivr.net/npm/' | ||
|
||
|
@@ -85,7 +84,6 @@ def SortableJS( | |
): | ||
src = """ | ||
import {Sortable} from 'https://cdn.jsdelivr.net/npm/sortablejs/+esm'; | ||
import {proc_htmx} from "https://cdn.jsdelivr.net/gh/answerdotai/[email protected]/fasthtml.js"; | ||
proc_htmx('%s', Sortable.create); | ||
""" % sel | ||
return Script(src, type='module') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/06_jupyter.ipynb. | ||
|
||
# %% auto 0 | ||
__all__ = ['cors_allow', 'nb_serve', 'nb_serve_async', 'is_port_free', 'wait_port_free', 'JupyUvi', 'FastJupy', 'HTMX'] | ||
|
||
# %% ../nbs/api/06_jupyter.ipynb | ||
import asyncio, socket, time, uvicorn | ||
from threading import Thread | ||
from fastcore.utils import * | ||
from .core import * | ||
from .components import * | ||
from .xtend import * | ||
from IPython.display import HTML,Markdown,IFrame | ||
from starlette.middleware.cors import CORSMiddleware | ||
from starlette.middleware import Middleware | ||
from fastcore.parallel import startthread | ||
|
||
# %% ../nbs/api/06_jupyter.ipynb | ||
def nb_serve(app, log_level="error", port=8000, **kwargs): | ||
"Start a Jupyter compatible uvicorn server with ASGI `app` on `port` with `log_level`" | ||
server = uvicorn.Server(uvicorn.Config(app, log_level=log_level, port=port, **kwargs)) | ||
async def async_run_server(server): await server.serve() | ||
@startthread | ||
def run_server(): asyncio.run(async_run_server(server)) | ||
while not server.started: time.sleep(0.01) | ||
return server | ||
|
||
# %% ../nbs/api/06_jupyter.ipynb | ||
async def nb_serve_async(app, log_level="error", port=8000, **kwargs): | ||
"Async version of `nb_serve`" | ||
server = uvicorn.Server(uvicorn.Config(app, log_level=log_level, port=port, **kwargs)) | ||
asyncio.get_running_loop().create_task(server.serve()) | ||
while not server.started: await asyncio.sleep(0.01) | ||
return server | ||
|
||
# %% ../nbs/api/06_jupyter.ipynb | ||
def is_port_free(port, host='localhost'): | ||
"Check if `port` is free on `host`" | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
try: | ||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
sock.bind((host, port)) | ||
return True | ||
except OSError: return False | ||
finally: sock.close() | ||
|
||
# %% ../nbs/api/06_jupyter.ipynb | ||
def wait_port_free(port, host='localhost', max_wait=3): | ||
"Wait for `port` to be free on `host`" | ||
start_time = time.time() | ||
while not is_port_free(port): | ||
if time.time() - start_time>max_wait: return print(f"Timeout") | ||
time.sleep(0.1) | ||
|
||
# %% ../nbs/api/06_jupyter.ipynb | ||
cors_allow = Middleware(CORSMiddleware, allow_credentials=True, | ||
allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]) | ||
|
||
# %% ../nbs/api/06_jupyter.ipynb | ||
class JupyUvi: | ||
"Start and stop a Jupyter compatible uvicorn server with ASGI `app` on `port` with `log_level`" | ||
def __init__(self, app, log_level="error", port=8000, start=True, **kwargs): | ||
self.kwargs = kwargs | ||
store_attr(but='start') | ||
self.server = None | ||
if start: self.start() | ||
|
||
def start(self): | ||
self.server = nb_serve(self.app, log_level=self.log_level, port=self.port, **self.kwargs) | ||
|
||
def stop(self): | ||
self.server.should_exit = True | ||
wait_port_free(self.port) | ||
|
||
# %% ../nbs/api/06_jupyter.ipynb | ||
# The script lets an iframe parent know of changes so that it can resize automatically. | ||
_iframe_scr = Script(""" | ||
function sendmsg() {window.parent.postMessage({height: document.documentElement.offsetHeight}, '*')} | ||
window.onload = function() { | ||
sendmsg(); | ||
document.body.addEventListener('htmx:afterSettle', sendmsg); | ||
};""") | ||
|
||
# %% ../nbs/api/06_jupyter.ipynb | ||
def FastJupy(hdrs=None, middleware=None, **kwargs): | ||
"Same as FastHTML, but with Jupyter compatible middleware and headers added" | ||
hdrs = listify(hdrs)+[_iframe_scr] | ||
middleware = listify(middleware)+[cors_allow] | ||
return FastHTML(hdrs=hdrs, middleware=middleware, **kwargs) | ||
|
||
# %% ../nbs/api/06_jupyter.ipynb | ||
def HTMX(host='localhost', port=8000): | ||
"An iframe which displays the HTMX application in a notebook." | ||
return HTML(f'<iframe src="http://{host}:{port}" ' + """style="width: 100%; border: none;" onload="{ | ||
let frame = this; | ||
window.addEventListener('message', function(e) { | ||
if (e.data.height) frame.style.height = (e.data.height+1) + 'px'; | ||
}, false); | ||
}"></iframe> """) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js"; | ||
import { proc_htmx } from "https://cdn.jsdelivr.net/gh/answerdotai/[email protected]/fasthtml.js"; | ||
import katex from "https://cdn.jsdelivr.net/npm/katex/dist/katex.mjs"; | ||
|
||
const renderMath = (tex, displayMode) => { return katex.renderToString(tex, { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,7 +129,6 @@ | |
"source": [ | ||
"#| export\n", | ||
"marked_imp = \"\"\"import { marked } from \"https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js\";\n", | ||
" import { proc_htmx } from \"https://cdn.jsdelivr.net/gh/answerdotai/[email protected]/fasthtml.js\";\n", | ||
"\"\"\"\n", | ||
"npmcdn = 'https://cdn.jsdelivr.net/npm/'" | ||
] | ||
|
@@ -265,7 +264,6 @@ | |
" ):\n", | ||
" src = \"\"\"\n", | ||
"import {Sortable} from 'https://cdn.jsdelivr.net/npm/sortablejs/+esm';\n", | ||
"import {proc_htmx} from \"https://cdn.jsdelivr.net/gh/answerdotai/[email protected]/fasthtml.js\";\n", | ||
"proc_htmx('%s', Sortable.create);\n", | ||
"\"\"\" % sel\n", | ||
" return Script(src, type='module')" | ||
|
Oops, something went wrong.