Skip to content

Commit d88f51c

Browse files
committed
Replite: Fix multiline support
1 parent 4e19c54 commit d88f51c

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

docs/replite.rst

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,28 @@ This directive takes the same options as the ``replite`` package, see https://gi
88
99
.. replite::
1010
:kernel: python
11-
:toolbar: 1
12-
:theme: JupyterLab Light
13-
:width: 100%
1411
:height: 600px
1512
16-
print('Hello, World!')
13+
import matplotlib.pyplot as plt
14+
import numpy as np
15+
16+
x = np.linspace(0, 2 * np.pi, 200)
17+
y = np.sin(x)
18+
19+
fig, ax = plt.subplots()
20+
ax.plot(x, y)
21+
plt.show()
1722
1823
.. replite::
1924
:kernel: python
20-
:toolbar: 1
21-
:theme: JupyterLab Light
22-
:width: 100%
2325
:height: 600px
2426

25-
print('Hello, World!')
27+
import matplotlib.pyplot as plt
28+
import numpy as np
29+
30+
x = np.linspace(0, 2 * np.pi, 200)
31+
y = np.sin(x)
32+
33+
fig, ax = plt.subplots()
34+
ax.plot(x, y)
35+
plt.show()

src/jupyterlite_sphinx.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,26 @@ def __init__(
4343
*children,
4444
width="100%",
4545
height="100%",
46+
content=[],
4647
replite_options=None,
4748
**attributes,
4849
):
4950
super().__init__(
50-
"", width=width, height=height, replite_options=replite_options
51+
"",
52+
width=width,
53+
height=height,
54+
content=content,
55+
replite_options=replite_options,
5156
)
5257

5358
def html(self):
5459
replite_options = self["replite_options"]
60+
61+
code_lines = [line.strip().replace(" ", "%20") for line in self["content"]]
62+
code = "%0A".join(code_lines)
63+
64+
replite_options["code"] = code
65+
5566
options = "&".join([f"{key}={value}" for key, value in replite_options.items()])
5667

5768
return (
@@ -80,10 +91,14 @@ def run(self):
8091
width = self.options.pop("width", "100%")
8192
height = self.options.pop("height", "100%")
8293

83-
if self.content:
84-
self.options["code"] = "".join(self.content)
85-
86-
return [RepliteIframe(width=width, height=height, replite_options=self.options)]
94+
return [
95+
RepliteIframe(
96+
width=width,
97+
height=height,
98+
content=self.content,
99+
replite_options=self.options,
100+
)
101+
]
87102

88103

89104
class _LiteIframe(Element):

0 commit comments

Comments
 (0)