mirror of
https://github.com/pyscript/pyscript.git
synced 2026-02-27 11:04:05 -05:00
Better test support for Python Plugins (#1108)
* add plugins testing utils module * add plugins manager fixture and init plugins tests helper in conftest * add _custom_elements attribute to pyscript.Plugin to allow plugins to track the CE they register * add test for py_tutor * remove unrelated code from prims js script * ensure a Plugin always has the app attribute and improve tests * add tests for py_tutor create_code_section * implement PluginsManager reset and add teardown on plugins_manager fixture to clean it up after a test * add test to check if plugin has been registered * add docstrings to new tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add docstrings to plugins tester * add changes from main * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add todo to add remaining PluginsManager lifecycle events Co-authored-by: Fabio Pliger <fpliger@anaconda.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -90,10 +90,6 @@ class PyTutor:
|
||||
# Add the JS file
|
||||
script = js.document.createElement("script")
|
||||
script.type = "text/javascript"
|
||||
try:
|
||||
script.appendChild(js.document.createTextNode(PAGE_SCRIPT))
|
||||
except BaseException:
|
||||
script.text = PAGE_SCRIPT
|
||||
script.src = "./assets/prism/prism.js"
|
||||
js.document.head.appendChild(script)
|
||||
|
||||
|
||||
@@ -494,11 +494,27 @@ class Plugin:
|
||||
name = self.__class__.__name__
|
||||
|
||||
self.name = name
|
||||
self._custom_elements = []
|
||||
self.app = None
|
||||
|
||||
def init(self, app):
|
||||
self.app = app
|
||||
|
||||
def register_custom_element(self, tag):
|
||||
"""
|
||||
Decorator to register a new custom element as part of a Plugin and associate
|
||||
tag to it. Internally, it delegates the registration to the PyScript internal
|
||||
[JS] plugin manager, who actually creates the JS custom element that can be
|
||||
attached to the page and instantiate an instance of the class passing the custom
|
||||
element to the plugin constructor.
|
||||
|
||||
Exammple:
|
||||
>> plugin = Plugin("PyTutorial")
|
||||
>> @plugin.register_custom_element("py-tutor")
|
||||
>> class PyTutor:
|
||||
>> def __init__(self, element):
|
||||
>> self.element = element
|
||||
"""
|
||||
# TODO: Ideally would be better to use the logger.
|
||||
js.console.info(f"Defining new custom element {tag}")
|
||||
|
||||
@@ -507,6 +523,7 @@ class Plugin:
|
||||
# until we have JS interface that works across interpreters
|
||||
define_custom_element(tag, create_proxy(class_)) # noqa: F821
|
||||
|
||||
self._custom_elements.append(tag)
|
||||
return create_proxy(wrapper)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user