Deprecate pyscript output attribute (#981)

* Deprecate pyscript output attribute

* Update pyscriptjs/src/components/pyscript.ts

Co-authored-by: Antonio Cuni <anto.cuni@gmail.com>

Co-authored-by: Antonio Cuni <anto.cuni@gmail.com>
This commit is contained in:
Fábio Rosado
2022-11-25 18:04:27 +00:00
committed by GitHub
parent b062efcf17
commit 446c131ccb
5 changed files with 33 additions and 14 deletions

View File

@@ -131,8 +131,8 @@ If your `.whl` is not a pure Python wheel, then open a PR or issue with [pyodide
If there's enough popular demand, the pyodide team will likely work on supporting your package. Regardless, things will likely move faster if you make the PR and consult with the team to get unblocked.
For example, NumPy and Matplotlib are available. Notice here we're using `<py-script output="plot">`
as a shortcut, which takes the expression on the last line of the script and runs `pyscript.write('plot', fig)`.
For example, NumPy and Matplotlib are available. Notice here we're using `display(fig, target="plot")`, which takes the graph and displays it in the element with the id `plot`.
```html
<html>
@@ -149,14 +149,14 @@ as a shortcut, which takes the expression on the last line of the script and run
"packages": ["numpy", "matplotlib"]
}
</py-config>
<py-script output="plot">
<py-script>
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(1000)
y = np.random.randn(1000)
fig, ax = plt.subplots()
ax.scatter(x, y)
fig
display(fig, target="plot")
</py-script>
</body>
</html>
@@ -196,13 +196,13 @@ In the HTML tag `<py-config>`, paths to local modules are provided in the
[[fetch]]
files = ["./data.py"]
</py-config>
<py-script output="plot">
<py-script>
import matplotlib.pyplot as plt
from data import make_x_and_y
x, y = make_x_and_y(n=1000)
fig, ax = plt.subplots()
ax.scatter(x, y)
fig
display(fig, target="plot")
</py-script>
</body>
</html>

View File

@@ -269,8 +269,7 @@ If your `.whl` is not a pure Python wheel, then open a PR or issue with [pyodide
If there's enough popular demand, the pyodide team will likely work on supporting your package. Regardless, things will likely move faster if you make the PR and consult with the team to get unblocked.
For example, NumPy and Matplotlib are available. Notice here we're using `<py-script output="plot">`
as a shortcut, which takes the expression on the last line of the script and runs `pyscript.write('plot', fig)`.
For example, NumPy and Matplotlib are available. Notice here we're using `display(fig, target="plot")`, which takes the graph and displays it in the element with the id `plot`.
```html
<html>
@@ -287,14 +286,14 @@ as a shortcut, which takes the expression on the last line of the script and run
"packages": ["numpy", "matplotlib"]
}
</py-config>
<py-script output="plot">
<py-script>
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(1000)
y = np.random.randn(1000)
fig, ax = plt.subplots()
ax.scatter(x, y)
fig
display(fig, target="plot")
</py-script>
</body>
</html>
@@ -356,13 +355,13 @@ In the HTML tag `<py-config>`, paths to local modules are provided in the
[[fetch]]
files = ["./data.py"]
</py-config>
<py-script output="plot">
<py-script>
import matplotlib.pyplot as plt
from data import make_x_and_y
x, y = make_x_and_y(n=1000)
fig, ax = plt.subplots()
ax.scatter(x, y)
fig
display(fig, target="plot")
</py-script>
</body>
</html>

View File

@@ -32,7 +32,7 @@
[[fetch]]
files = ["./utils.py"]
</py-config>
<py-script output="outputDiv">
<py-script>
import utils
display(utils.now())
</py-script>

View File

@@ -1,4 +1,4 @@
import { htmlDecode, ensureUniqueId } from '../utils';
import { htmlDecode, ensureUniqueId, showWarning } from '../utils';
import type { Runtime } from '../runtime';
import { getLogger } from '../logger';
import { pyExec } from '../pyexec';
@@ -10,6 +10,14 @@ const logger = getLogger('py-script');
export function make_PyScript(runtime: Runtime) {
class PyScript extends HTMLElement {
async connectedCallback() {
if (this.hasAttribute('output')) {
const deprecationMessage = (
"The 'output' attribute is deprecated and ignored. You should use " +
"'display()' to output the content to a specific element. " +
'For example display(myElement, target="divID").'
)
showWarning(deprecationMessage)
}
ensureUniqueId(this);
const pySrc = await this.getPySrc();
this.innerHTML = '';

View File

@@ -34,6 +34,18 @@ class TestOutput(PyScriptTest):
lines = [line for line in lines if line != ""] # remove empty lines
assert lines == ["hello 1", "hello 2", "hello 3"]
def test_output_attribute_shows_deprecated_warning(self):
self.pyscript_run(
"""
<py-script output="myDiv">
display('hello world')
</py-script>
<div id="mydiv"></div>
"""
)
warning_banner = self.page.locator(".alert-banner")
assert "The 'output' attribute is deprecated" in warning_banner.inner_text()
def test_target_attribute(self):
self.pyscript_run(
"""