import create_proxy and to_js from pyodide.ffi (#725)

This commit is contained in:
Madhur Tandon
2022-08-26 01:41:18 +05:30
committed by GitHub
parent 0f50f4a9fd
commit edfd4baa1f
8 changed files with 10 additions and 11 deletions

View File

@@ -2,7 +2,7 @@ import random
import sys import sys
from js import DOMParser, document, setInterval from js import DOMParser, document, setInterval
from pyodide import create_proxy from pyodide.ffi import create_proxy
from pyodide.http import open_url from pyodide.http import open_url

View File

@@ -71,17 +71,17 @@ def _link_docs(pydoc, jsdoc):
if getattr(event, 'setter_id', None) is not None: if getattr(event, 'setter_id', None) is not None:
return return
events = [event] events = [event]
json_patch = jsdoc.create_json_patch_string(pyodide.to_js(events)) json_patch = jsdoc.create_json_patch_string(pyodide.ffi.to_js(events))
pydoc.apply_json_patch(json.loads(json_patch)) pydoc.apply_json_patch(json.loads(json_patch))
jsdoc.on_change(pyodide.create_proxy(jssync), pyodide.to_js(False)) jsdoc.on_change(pyodide.ffi.create_proxy(jssync), pyodide.ffi.to_js(False))
def pysync(event): def pysync(event):
json_patch, buffers = process_document_events([event], use_buffers=True) json_patch, buffers = process_document_events([event], use_buffers=True)
buffer_map = {} buffer_map = {}
for (ref, buffer) in buffers: for (ref, buffer) in buffers:
buffer_map[ref['id']] = buffer buffer_map[ref['id']] = buffer
jsdoc.apply_json_patch(JSON.parse(json_patch), pyodide.to_js(buffer_map), setter_id='js') jsdoc.apply_json_patch(JSON.parse(json_patch), pyodide.ffi.to_js(buffer_map), setter_id='js')
pydoc.on_change(pysync) pydoc.on_change(pysync)

View File

@@ -109,7 +109,7 @@ for (const d of data) {
</script> </script>
<py-script> <py-script>
from pyodide import create_proxy, to_js from pyodide.ffi import create_proxy, to_js
import d3 import d3
fruits = [ fruits = [

View File

@@ -87,7 +87,7 @@
</py-env> </py-env>
<py-script> <py-script>
from pyodide import to_js, create_proxy from pyodide.ffi import to_js, create_proxy
import numpy as np import numpy as np
import sympy import sympy

View File

@@ -28,12 +28,11 @@
</script> </script>
<py-script> <py-script>
from pyodide import create_proxy, to_js from pyodide.ffi import create_proxy, to_js
from js import window from js import window
from js import Math from js import Math
from js import THREE from js import THREE
from js import performance from js import performance
from pyodide import to_js
from js import Object from js import Object

View File

@@ -48,7 +48,7 @@ export class PyButton extends BaseEvalElement {
this.appendChild(mainDiv); this.appendChild(mainDiv);
this.code = this.code.split('self').join(this.mount_name); this.code = this.code.split('self').join(this.mount_name);
let registrationCode = `from pyodide import create_proxy`; let registrationCode = `from pyodide.ffi import create_proxy`;
registrationCode += `\n${this.mount_name} = Element("${mainDiv.id}")`; registrationCode += `\n${this.mount_name} = Element("${mainDiv.id}")`;
if (this.code.includes('def on_focus')) { if (this.code.includes('def on_focus')) {
this.code = this.code.replace('def on_focus', `def on_focus_${this.mount_name}`); this.code = this.code.replace('def on_focus', `def on_focus_${this.mount_name}`);

View File

@@ -31,7 +31,7 @@ export class PyInputBox extends BaseEvalElement {
// defined for this widget // defined for this widget
this.appendChild(mainDiv); this.appendChild(mainDiv);
this.code = this.code.split('self').join(this.mount_name); this.code = this.code.split('self').join(this.mount_name);
let registrationCode = `from pyodide import create_proxy`; let registrationCode = `from pyodide.ffi import create_proxy`;
registrationCode += `\n${this.mount_name} = Element("${mainDiv.id}")`; registrationCode += `\n${this.mount_name} = Element("${mainDiv.id}")`;
if (this.code.includes('def on_keypress')) { if (this.code.includes('def on_keypress')) {
this.code = this.code.replace('def on_keypress', `def on_keypress_${this.mount_name}`); this.code = this.code.replace('def on_keypress', `def on_keypress_${this.mount_name}`);

View File

@@ -227,7 +227,7 @@ async function createElementsWithEventListeners(runtime: Runtime, pyAttribute: s
const handlerCode = el.getAttribute(pyAttribute); const handlerCode = el.getAttribute(pyAttribute);
const event = pyAttributeToEvent.get(pyAttribute); const event = pyAttributeToEvent.get(pyAttribute);
const source = ` const source = `
from pyodide import create_proxy from pyodide.ffi import create_proxy
Element("${el.id}").element.addEventListener("${event}", create_proxy(${handlerCode})) Element("${el.id}").element.addEventListener("${event}", create_proxy(${handlerCode}))
`; `;
await runtime.run(source); await runtime.run(source);