8000 getPySrc should return source code (#1041) · WilliamHackspeare/pyscript@08e83fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 08e83fe

Browse files
getPySrc should return source code (pyscript#1041)
* getPySrc should return source code * fix lint * add test * fix dynamic tag test * address feedback
1 parent 4f05b5a commit 08e83fe

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pyscriptjs/src/components/pyscript.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const logger = getLogger('py-script');
99

1010
export function make_PyScript(runtime: Runtime) {
1111
class PyScript extends HTMLElement {
12+
srcCode: string
13+
1214
async connectedCallback() {
1315
if (this.hasAttribute('output')) {
1416
const deprecationMessage = (
@@ -19,6 +21,10 @@ export function make_PyScript(runtime: Runtime) {
1921
showWarning(deprecationMessage)
2022
}
2123
ensureUniqueId(this);
24+
// Save innerHTML information in srcCode so we can access it later
25+
// once we clean innerHTML (which is required since we don't want
26+
// source code to be rendered on the screen)
27+
this.srcCode = this.innerHTML;
2228
const pySrc = await this.getPySrc();
2329
this.innerHTML = '';
2430
pyExec(runtime, pySrc, this);
@@ -36,7 +42,7 @@ export function make_PyScript(runtime: Runtime) {
3642
throw e
3743
}
3844
} else {
39-
return htmlDecode(this.innerHTML);
45+
return htmlDecode(this.srcCode);
4046
}
4147
}
4248
}

pyscriptjs/tests/integration/test_01_basic.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,19 @@ def test_deprecated_globals(self):
276276
"Direct usage of sys is deprecated. Please use import sys instead.",
277277
"Direct usage of create is deprecated. Please use pyscript.create instead.",
278278
]
279+
280+
def test_getPySrc_returns_source_code(self):
281+
self.pyscript_run(
282+
"""
283+
<py-script>
284+
print("hello world!")
285+
</py-script>
286+
"""
287+
)
288+
289+
pyscript_tag = self.page.locator("py-script")
290+
assert pyscript_tag.inner_html() == ""
291+
assert (
292+
pyscript_tag.evaluate("node => node.getPySrc()")
293+
== 'print("hello world!")\n'
294+
)

0 commit comments

Comments
 (0)
0