def test_to_js_default_converter2(selenium): import json import pytest from js import JSON, Array from pyodide import JsException, run_js, to_js class Pair: def __init__(self, first, second): self.first = first self.second = second p1 = Pair(1, 2) p2 = Pair(1, 2) p2.first = p2 def default_converter(value, convert, cacheConversion): result = Array.new() cacheConversion(value, result) result.push(convert(value.first)) result.push(convert(value.second)) return result p1js = to_js(p1, default_converter=default_converter) p2js = to_js(p2, default_converter=default_converter) assert json.loads(JSON.stringify(p1js)) == [1, 2] with pytest.raises(JsException, match="TypeError"): JSON.stringify(p2js) assert run_js("(x) => x[0] === x")(p2js) assert run_js("(x) => x[1] === 2")(p2js)
def pysync(event): json_patch, buffers = process_document_events([event], use_buffers=True) buffer_map = {} for (ref, buffer) in buffers: buffer_map[ref['id']] = pyodide.to_js(buffer).buffer jsdoc.apply_json_patch(JSON.parse(json_patch), pyodide.to_js(buffer_map), setter_id='js')
async def add_ui(self, source_id, ui): # print("Handling new user...") # Might as well make sure that we don't have other charts that are out of date. # So let's send an update to the existing charts first. await self.update_a() self.ui_tabs[source_id] = ui # "initialize" command sets chart range and page in addition to setting the chart. # "initialize" does a superset of what "reset" does. try: await ui.initializeSseq(js_JSON.parse(JSON.stringify(self.chart))) except Exception as e: console.log(e)
def load_config(): config = None config_str = localStorage.getItem('config') if config_str: config = JSON.parse(config_str) config = config or { 'use_tls': True, 'couch_host': '', 'couch_port': '', 'couch_username': '', 'couch_password': '', 'sync_enabled': True, } return config
async def export_data(self): data = {} for db_name in ['recipes', 'plans', 'groceries']: all_docs = await PromiseProxy(PouchDB.new(db_name).allDocs({'include_docs': True})) data[db_name] = [row.doc for row in all_docs.rows] blob = Blob.new([JSON.stringify(data)], {'type': 'application/json'}) print('blob made', blob) url = URL.createObjectURL(blob) print('made url', url) link = document.createElement('a') link.setAttribute('href', url) link.setAttribute('download', 'nutrition_export.json') document.body.appendChild(link) link.click() link.remove() URL.revokeObjectURL(url)
async def show(obj, target): """ Renders the object into a DOM node specified by the target. Arguments --------- obj: Viewable Object to render into the DOM node target: str Target ID of the DOM node to render the object into. """ from js import Bokeh from ..pane import panel as as_panel obj = as_panel(obj) pydoc, model_json = _doc_json(obj, target) views = await Bokeh.embed.embed_item(JSON.parse(model_json)) jsdoc = views[0].model.document _link_docs(pydoc, jsdoc)
def main(): kv = get_config() print JSON().encode(kv)
async def update_charts_a(self, messages): messages = js_JSON.parse(JSON.stringify(messages)) for ui in self.ui_tabs.values(): await ui.appplyMessages(messages)
def main(): print JSON().encode(get_config())
def save_config(config): localStorage.setItem('config', JSON.stringify(config)) sync.configure(config)