Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def _resp(req, resp, cls=empty, status_code=200):
"Create appropriate HTTP response from request and response data"
if not resp: resp=''
if hasattr(resp, '__response__'): resp = resp.__response__(req)
if cls in (Any,FT): cls=empty
if not (isinstance(cls, type) and issubclass(cls, Response)): cls=empty
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Any, and FT checks are covered in isisntance type check. the type checking is required or else issubclass Response will throw an error.

if isinstance(resp, FileResponse) and not os.path.exists(resp.path): raise HTTPException(404, resp.path)
resp,kw = _part_resp(req, resp)
if isinstance(resp, Response): return resp
Expand Down
18 changes: 9 additions & 9 deletions fasthtml/js.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@
__all__ = ['marked_imp', 'npmcdn', 'light_media', 'dark_media', 'MarkdownJS', 'KatexMarkdownJS', 'HighlightJS', 'SortableJS',
'MermaidJS']

# %% ../nbs/api/03_js.ipynb #ddc8b61a
# %% ../nbs/api/03_js.ipynb #cd3eb2a3
import re
from fastcore.utils import *
from fasthtml.components import *
from fasthtml.xtend import *

# %% ../nbs/api/03_js.ipynb #4c8d1272
# %% ../nbs/api/03_js.ipynb #f7792683
def light_media(
css: str # CSS to be included in the light media query
):
"Render light media for day mode views"
return Style('@media (prefers-color-scheme: light) {%s}' %css)

# %% ../nbs/api/03_js.ipynb #26cd2ccc
# %% ../nbs/api/03_js.ipynb #0a2307c3
def dark_media(
css: str # CSS to be included in the dark media query
):
"Render dark media for night mode views"
return Style('@media (prefers-color-scheme: dark) {%s}' %css)

# %% ../nbs/api/03_js.ipynb #c7cd64ef
# %% ../nbs/api/03_js.ipynb #f60ca53b
marked_imp = """import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js";
"""
npmcdn = 'https://cdn.jsdelivr.net/npm/'

# %% ../nbs/api/03_js.ipynb #857e3fe6
# %% ../nbs/api/03_js.ipynb #3e318ccd
def MarkdownJS(
sel='.marked' # CSS selector for markdown elements
):
"Implements browser-based markdown rendering."
src = "proc_htmx('%s', e => e.innerHTML = marked.parse(e.textContent));" % sel
return Script(marked_imp+src, type='module')

# %% ../nbs/api/03_js.ipynb #fa5b6642
# %% ../nbs/api/03_js.ipynb #595d4e8f
def KatexMarkdownJS(
sel='.marked', # CSS selector for markdown elements
inline_delim='$', # Delimiter for inline math
Expand All @@ -54,7 +54,7 @@ def KatexMarkdownJS(
css = Link(rel="stylesheet", href=npmcdn+"katex@0.16.11/dist/katex.min.css")
return scr,css

# %% ../nbs/api/03_js.ipynb #c8144d89
# %% ../nbs/api/03_js.ipynb #7a2a45ef
def HighlightJS(
sel='pre code:not([data-highlighted="yes"])', # CSS selector for code elements. Default is industry standard, be careful before adjusting it
langs:str|list|tuple='python', # Language(s) to highlight
Expand All @@ -77,7 +77,7 @@ def HighlightJS(
jsd(*hjc, 'highlightjs-copy.min.css', typ='css'),
*langjs, Script(src, type='module')]

# %% ../nbs/api/03_js.ipynb #9004c11b
# %% ../nbs/api/03_js.ipynb #59c00b7d
def SortableJS(
sel='.sortable', # CSS selector for sortable elements
ghost_class='blue-background-class' # When an element is being dragged, this is the class used to distinguish it from the rest
Expand All @@ -88,7 +88,7 @@ def SortableJS(
""" % sel
return Script(src, type='module')

# %% ../nbs/api/03_js.ipynb #0e060ddb
# %% ../nbs/api/03_js.ipynb #3518340d
def MermaidJS(
sel='.language-mermaid', # CSS selector for mermaid elements
theme='base', # Mermaid theme to use
Expand Down
10 changes: 2 additions & 8 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,7 @@
" \"Create appropriate HTTP response from request and response data\"\n",
" if not resp: resp=''\n",
" if hasattr(resp, '__response__'): resp = resp.__response__(req)\n",
" if cls in (Any,FT): cls=empty\n",
" if not (isinstance(cls, type) and issubclass(cls, Response)): cls=empty\n",
" if isinstance(resp, FileResponse) and not os.path.exists(resp.path): raise HTTPException(404, resp.path)\n",
" resp,kw = _part_resp(req, resp)\n",
" if isinstance(resp, Response): return resp\n",
Expand Down Expand Up @@ -3963,13 +3963,7 @@
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "python3",
"language": "python",
"name": "python3"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}