mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-19 18:27:29 -05:00
getPySrc should return source code (#1041)
* getPySrc should return source code * fix lint * add test * fix dynamic tag test * address feedback
This commit is contained in:
@@ -9,6 +9,8 @@ const logger = getLogger('py-script');
|
||||
|
||||
export function make_PyScript(runtime: Runtime) {
|
||||
class PyScript extends HTMLElement {
|
||||
srcCode: string
|
||||
|
||||
async connectedCallback() {
|
||||
if (this.hasAttribute('output')) {
|
||||
const deprecationMessage = (
|
||||
@@ -19,6 +21,10 @@ export function make_PyScript(runtime: Runtime) {
|
||||
showWarning(deprecationMessage)
|
||||
}
|
||||
ensureUniqueId(this);
|
||||
// Save innerHTML information in srcCode so we can access it later
|
||||
// once we clean innerHTML (which is required since we don't want
|
||||
// source code to be rendered on the screen)
|
||||
this.srcCode = this.innerHTML;
|
||||
const pySrc = await this.getPySrc();
|
||||
this.innerHTML = '';
|
||||
pyExec(runtime, pySrc, this);
|
||||
@@ -36,7 +42,7 @@ export function make_PyScript(runtime: Runtime) {
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
return htmlDecode(this.innerHTML);
|
||||
return htmlDecode(this.srcCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,3 +276,19 @@ class TestBasic(PyScriptTest):
|
||||
"Direct usage of sys is deprecated. Please use import sys instead.",
|
||||
"Direct usage of create is deprecated. Please use pyscript.create instead.",
|
||||
]
|
||||
|
||||
def test_getPySrc_returns_source_code(self):
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<py-script>
|
||||
print("hello world!")
|
||||
</py-script>
|
||||
"""
|
||||
)
|
||||
|
||||
pyscript_tag = self.page.locator("py-script")
|
||||
assert pyscript_tag.inner_html() == ""
|
||||
assert (
|
||||
pyscript_tag.evaluate("node => node.getPySrc()")
|
||||
== 'print("hello world!")\n'
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user