mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-23 04:03:00 -05:00
add config file for pre-commit (#235)
* add config file * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add isort * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,57 +1,61 @@
|
||||
from js import document, console
|
||||
import asyncio
|
||||
import io, base64
|
||||
import base64
|
||||
import io
|
||||
|
||||
from js import console, document
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
MIME_METHODS = {
|
||||
'__repr__': 'text/plain',
|
||||
'_repr_html_': 'text/html',
|
||||
'_repr_markdown_': 'text/markdown',
|
||||
'_repr_svg_': 'image/svg+xml',
|
||||
'_repr_png_': 'image/png',
|
||||
'_repr_pdf_': 'application/pdf',
|
||||
'_repr_jpeg_': 'image/jpeg',
|
||||
'_repr_latex': 'text/latex',
|
||||
'_repr_json_': 'application/json',
|
||||
'_repr_javascript_': 'application/javascript',
|
||||
'savefig': 'image/png'
|
||||
"__repr__": "text/plain",
|
||||
"_repr_html_": "text/html",
|
||||
"_repr_markdown_": "text/markdown",
|
||||
"_repr_svg_": "image/svg+xml",
|
||||
"_repr_png_": "image/png",
|
||||
"_repr_pdf_": "application/pdf",
|
||||
"_repr_jpeg_": "image/jpeg",
|
||||
"_repr_latex": "text/latex",
|
||||
"_repr_json_": "application/json",
|
||||
"_repr_javascript_": "application/javascript",
|
||||
"savefig": "image/png",
|
||||
}
|
||||
|
||||
|
||||
def render_image(mime, value, meta):
|
||||
data = f'data:{mime};charset=utf-8;base64,{value}'
|
||||
attrs = ' '.join(['{k}="{v}"' for k, v in meta.items()])
|
||||
data = f"data:{mime};charset=utf-8;base64,{value}"
|
||||
attrs = " ".join(['{k}="{v}"' for k, v in meta.items()])
|
||||
return f'<img src="{data}" {attrs}</img>'
|
||||
|
||||
|
||||
def identity(value, meta):
|
||||
return value
|
||||
|
||||
|
||||
MIME_RENDERERS = {
|
||||
'text/plain': identity,
|
||||
'text/html' : identity,
|
||||
'image/png' : lambda value, meta: render_image('image/png', value, meta),
|
||||
'image/jpeg': lambda value, meta: render_image('image/jpeg', value, meta),
|
||||
'image/svg+xml': identity,
|
||||
'application/json': identity,
|
||||
'application/javascript': lambda value, meta: f'<script>{value}</script>'
|
||||
}
|
||||
"text/plain": identity,
|
||||
"text/html": identity,
|
||||
"image/png": lambda value, meta: render_image("image/png", value, meta),
|
||||
"image/jpeg": lambda value, meta: render_image("image/jpeg", value, meta),
|
||||
"image/svg+xml": identity,
|
||||
"application/json": identity,
|
||||
"application/javascript": lambda value, meta: f"<script>{value}</script>",
|
||||
}
|
||||
|
||||
|
||||
def eval_formatter(obj, print_method):
|
||||
"""
|
||||
Evaluates a formatter method.
|
||||
"""
|
||||
if print_method == '__repr__':
|
||||
return repr(obj)
|
||||
if print_method == "__repr__":
|
||||
return repr(obj)
|
||||
elif hasattr(obj, print_method):
|
||||
if print_method == 'savefig':
|
||||
if print_method == "savefig":
|
||||
buf = io.BytesIO()
|
||||
obj.savefig(buf, format='png')
|
||||
obj.savefig(buf, format="png")
|
||||
buf.seek(0)
|
||||
return base64.b64encode(buf.read()).decode('utf-8')
|
||||
return base64.b64encode(buf.read()).decode("utf-8")
|
||||
return getattr(obj, print_method)()
|
||||
elif print_method == '_repr_mimebundle_':
|
||||
elif print_method == "_repr_mimebundle_":
|
||||
return {}, {}
|
||||
return None
|
||||
|
||||
@@ -61,9 +65,9 @@ def format_mime(obj):
|
||||
Formats object using _repr_x_ methods.
|
||||
"""
|
||||
if isinstance(obj, str):
|
||||
return obj, 'text/plain'
|
||||
return obj, "text/plain"
|
||||
|
||||
mimebundle = eval_formatter(obj, '_repr_mimebundle_')
|
||||
mimebundle = eval_formatter(obj, "_repr_mimebundle_")
|
||||
if isinstance(mimebundle, tuple):
|
||||
format_dict, md_dict = mimebundle
|
||||
else:
|
||||
@@ -85,9 +89,11 @@ def format_mime(obj):
|
||||
break
|
||||
if output is None:
|
||||
if not_available:
|
||||
console.warning(f'Rendered object requested unavailable MIME renderers: {not_available}')
|
||||
console.warning(
|
||||
f"Rendered object requested unavailable MIME renderers: {not_available}"
|
||||
)
|
||||
output = repr(output)
|
||||
mime_type = 'text/plain'
|
||||
mime_type = "text/plain"
|
||||
elif isinstance(output, tuple):
|
||||
output, meta = output
|
||||
else:
|
||||
@@ -103,17 +109,17 @@ class PyScript:
|
||||
"""Writes value to the element with id "element_id"""
|
||||
console.log(f"APPENDING: {append} ==> {element_id} --> {value}")
|
||||
if append:
|
||||
child = document.createElement('div');
|
||||
element = document.querySelector(f'#{element_id}');
|
||||
child = document.createElement("div")
|
||||
element = document.querySelector(f"#{element_id}")
|
||||
if not element:
|
||||
return
|
||||
exec_id = exec_id or element.childElementCount + 1
|
||||
element_id = child.id = f"{element_id}-{exec_id}";
|
||||
element.appendChild(child);
|
||||
element_id = child.id = f"{element_id}-{exec_id}"
|
||||
element.appendChild(child)
|
||||
|
||||
element = document.getElementById(element_id)
|
||||
html, mime_type = format_mime(value)
|
||||
if mime_type in ('application/javascript', 'text/html'):
|
||||
if mime_type in ("application/javascript", "text/html"):
|
||||
scriptEl = document.createRange().createContextualFragment(html)
|
||||
element.appendChild(scriptEl)
|
||||
else:
|
||||
@@ -133,7 +139,7 @@ class Element:
|
||||
def element(self):
|
||||
"""Return the dom element"""
|
||||
if not self._element:
|
||||
self._element = document.querySelector(f'#{self._id}');
|
||||
self._element = document.querySelector(f"#{self._id}")
|
||||
return self._element
|
||||
|
||||
def write(self, value, append=False):
|
||||
@@ -143,8 +149,8 @@ class Element:
|
||||
pyscript.write(self._id, value, append=append)
|
||||
|
||||
def clear(self):
|
||||
if hasattr(self.element, 'value'):
|
||||
self.element.value = ''
|
||||
if hasattr(self.element, "value"):
|
||||
self.element.value = ""
|
||||
else:
|
||||
self.write("", append=False)
|
||||
|
||||
@@ -163,13 +169,13 @@ class Element:
|
||||
if new_id is None:
|
||||
new_id = self.element.id
|
||||
|
||||
clone = self.element.cloneNode(True);
|
||||
clone.id = new_id;
|
||||
clone = self.element.cloneNode(True)
|
||||
clone.id = new_id
|
||||
|
||||
if to:
|
||||
to.element.appendChild(clone)
|
||||
|
||||
# Inject it into the DOM
|
||||
self.element.after(clone);
|
||||
|
||||
self.element.after(clone)
|
||||
|
||||
return Element(clone.id, clone)
|
||||
|
||||
Reference in New Issue
Block a user