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:
Madhur Tandon
2022-12-13 02:41:21 +05:30
committed by GitHub
parent 4f05b5afc6
commit 08e83feaf5
2 changed files with 23 additions and 1 deletions

View File

@@ -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);
}
}
}

View File

@@ -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'
)