Skip to content

Commit c7cb9fa

Browse files
committed
Add a way to install modules in notebooks
Because we are parsing Python content to deliver rich color previews, we can't simply use micropip and await. When the code is processed, we are already in a loop and we have to run, at least when parsing Markdown, in synchronous functions. So, if processing code in a live code block, process `# micropip-require: module1 module2` as the first line. If processing a live notebook, use `<!-- micropip-require: module -->`, but at the top of the Markdown page and all code blocks will see it. You can also include it in the code blocks individually as it should be cached, that way if you want to share a block, it would have everything needed.
1 parent e84d9c9 commit c7cb9fa

File tree

11 files changed

+52
-15
lines changed

11 files changed

+52
-15
lines changed

docs/src/js/extra-notebook.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
let editTemp = {}
2020
const editorMgr = {}
2121
let notebookEditor = null
22+
const installMatch = /^#[ \t]*micropip-require:[ \t]*((?:[-a-z0-1_]+[ \t]*)+)(?=\n|$)/
23+
const installMdMatch = /^<!--[ \t]+micropip-require:[ \t]*((?:[-a-z0-1_]+[ \t]*)+)[ \t]+-->(?=\n|$)/
24+
const installPrefix = /^#[ \t]*micropip-require:[ \t]*/
25+
const installMdPrefix = /^<!--[ \t]+micropip-require:[ \t]*/
26+
const installMdPost = /[ \t]+-->(?=\n|$)/
2227
const reIdNum = /.*?_(\d+)$/
2328
let initialized = false
2429
let lastSearch = ""
@@ -92,7 +97,23 @@ ${content}
9297
const session = currentInputs.getAttribute("session")
9398
const editor = editorMgr[currentID]
9499
currentInputs.setAttribute("readonly", "")
95-
pyodide.globals.set('__pyodide_input__', editor.getValue())
100+
const value = editor.getValue()
101+
const match = installMatch.exec(value)
102+
if (match) {
103+
const installs = value.slice(match.index, match.index + match[0].length)
104+
.replace(installPrefix, '')
105+
.trim()
106+
.split(' ')
107+
try {
108+
const micropip = pyodide.pyimport('micropip.install')
109+
await micropip(installs)
110+
pyodide.globals.set("content", value.slice(match.index + match[0].length))
111+
} catch (err) {
112+
pyodide.globals.set("content", value)
113+
} // eslint-disable-line no-empty
114+
} else {
115+
pyodide.globals.set('content', value)
116+
}
96117
pyodide.globals.set("id_num", currentID)
97118
pyodide.globals.set("action", "playground")
98119
pyodide.globals.set("session_id", session)
@@ -122,7 +143,23 @@ ${content}
122143
if (main.getAttribute('livecode')) {
123144
main.removeAttribute('livecode')
124145
}
125-
pyodide.globals.set("content", text)
146+
const match = installMdMatch.exec(text)
147+
if (match) {
148+
const installs = text.slice(match.index, match.index + match[0].length)
149+
.replace(installMdPrefix, '')
150+
.replace(installMdPost, '')
151+
.trim()
152+
.split(' ')
153+
try {
154+
const micropip = pyodide.pyimport('micropip.install')
155+
await micropip(installs)
156+
pyodide.globals.set("content", text.slice(match.index + match[0].length))
157+
} catch (err) {
158+
pyodide.globals.set("content", text)
159+
} // eslint-disable-line no-empty
160+
} else {
161+
pyodide.globals.set("content", text)
162+
}
126163
pyodide.globals.set("action", "notebook")
127164
pyodide.globals.set('SESSIONS', null)
128165
await pyodide.runPythonAsync(pycode)

docs/src/mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ extra_css:
314314
extra_javascript:
315315
- https://cdn.jsdelivr.net/npm/ace-builds@1.39.0/src-min-noconflict/ace.js
316316
- https://cdn.jsdelivr.net/npm/mermaid@11.3.0/dist/mermaid.min.js
317-
- playground-config-e202a72e.js
317+
- playground-config-848efbd0.js
318318
- https://cdn.jsdelivr.net/pyodide/v0.26.4/full/pyodide.js
319319
- assets/coloraide-extras/extra-notebook.js
320320

docs/src/py/notebook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ def render_console(*args, **kwargs):
933933
results = document.getElementById("__playground-results_{}".format(globals()['id_num']))
934934
footer = document.querySelector("#__playground_{} .gamut".format(globals()['id_num']))
935935
session = globals()['session_id']
936-
result = live_color_command_formatter(LIVE_INIT, gamut)(globals()['__pyodide_input__'], session=session)
936+
result = live_color_command_formatter(LIVE_INIT, gamut)(globals()['content'], session=session)
937937
temp = document.createElement('div')
938938
temp.innerHTML = result
939939

docs/theme/assets/coloraide-extras/extra-notebook-B6JHkVzQ.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

docs/theme/assets/coloraide-extras/extra-notebook-B6JHkVzQ.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/theme/assets/coloraide-extras/extra-notebook-BuQ8qEc0.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/theme/assets/coloraide-extras/extra-notebook-BuQ8qEc0.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var colorNotebook = {
2+
"playgroundWheels": ['micropip', 'pygments-2.19.1-py3-none-any.whl', 'coloraide-4.2.1-py3-none-any.whl'],
3+
"notebookWheels": ['pyyaml', 'Markdown-3.7-py3-none-any.whl', 'pymdown_extensions-10.14.1-py3-none-any.whl', 'micropip', 'pygments-2.19.1-py3-none-any.whl', 'coloraide-4.2.1-py3-none-any.whl'],
4+
"defaultPlayground": "import coloraide\ncoloraide.__version__\nColor('red')"
5+
}

docs/theme/playground-config-e202a72e.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

mkdocs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,9 @@ extra_css:
314314
extra_javascript:
315315
- https://cdn.jsdelivr.net/npm/ace-builds@1.39.0/src-min-noconflict/ace.js
316316
- https://cdn.jsdelivr.net/npm/mermaid@11.3.0/dist/mermaid.min.js
317-
- playground-config-e202a72e.js
317+
- playground-config-848efbd0.js
318318
- https://cdn.jsdelivr.net/pyodide/v0.26.4/full/pyodide.js
319-
- assets/coloraide-extras/extra-notebook-B6JHkVzQ.js
319+
- assets/coloraide-extras/extra-notebook-BuQ8qEc0.js
320320

321321
extra:
322322
social:

0 commit comments

Comments
 (0)