Skip to content

Commit 36be39b

Browse files
authored
Fix missing !pip transform in notebook converter (#5841)
Fixes #5819 The `transform_exclamation_mark` transform was introduced in 59338be but was accidently omitted from the transforms pipeline (`_transform_sources`). The pipeline was later refactored in 92e6a04, but the transform remained missing. This caused `!pip` commands to end up in unparsable cells instead of being properly commented. Added the missing transform to the pipeline and test coverage. Note: The function name `transform_exclamation_mark` is a little misleading since it only handles `!pip` commands, not all exclamation-mark commands.
1 parent 31f1916 commit 36be39b

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

marimo/_convert/ipynb.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ def _transform_sources(
718718
source_transforms: list[Transform] = [
719719
transform_strip_whitespace,
720720
transform_magic_commands,
721+
transform_exclamation_mark,
721722
transform_remove_duplicate_imports,
722723
transform_fixup_multiple_definitions,
723724
transform_duplicate_definitions,

scripts/generate_ipynb_fixtures.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ def main() -> None:
156156
],
157157
)
158158

159+
create_notebook_fixture(
160+
"pip_commands",
161+
[
162+
"!pip install transformers",
163+
"!pip install pandas numpy matplotlib",
164+
"# Mixed cell with pip and other commands\n!pip install scikit-learn\nimport numpy as np\n!pip install seaborn",
165+
"# Non-pip exclamation commands should remain unchanged\n!ls -la\n!echo 'Hello World'",
166+
"# Magic pip command should also be handled\n%pip install requests",
167+
],
168+
)
169+
159170

160171
if __name__ == "__main__":
161172
main()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "0",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"!pip install transformers"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "1",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"!pip install pandas numpy matplotlib"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"id": "2",
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"# Mixed cell with pip and other commands\n",
31+
"!pip install scikit-learn\n",
32+
"import numpy as np\n",
33+
"!pip install seaborn"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"id": "3",
40+
"metadata": {},
41+
"outputs": [],
42+
"source": [
43+
"# Non-pip exclamation commands should remain unchanged\n",
44+
"!ls -la\n",
45+
"!echo 'Hello World'"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": null,
51+
"id": "4",
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"# Magic pip command should also be handled\n",
56+
"%pip install requests"
57+
]
58+
}
59+
],
60+
"metadata": {},
61+
"nbformat": 4,
62+
"nbformat_minor": 5
63+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import marimo
2+
3+
app = marimo.App()
4+
5+
6+
@app.cell
7+
def _():
8+
# (use marimo's built-in package management features instead) !pip install transformers
9+
return
10+
11+
12+
@app.cell
13+
def _():
14+
# (use marimo's built-in package management features instead) !pip install pandas numpy matplotlib
15+
return
16+
17+
18+
@app.cell
19+
def _():
20+
# Mixed cell with pip and other commands
21+
# (use marimo's built-in package management features instead) !pip install scikit-learn
22+
import numpy as np
23+
# (use marimo's built-in package management features instead) !pip install seaborn
24+
return
25+
26+
27+
app._unparsable_cell(
28+
r"""
29+
# Non-pip exclamation commands should remain unchanged
30+
!ls -la
31+
!echo 'Hello World'
32+
""",
33+
name="_"
34+
)
35+
36+
37+
@app.cell
38+
def _():
39+
# Magic pip command should also be handled
40+
# '%pip install requests' command supported automatically in marimo
41+
return
42+
43+
44+
if __name__ == "__main__":
45+
app.run()

0 commit comments

Comments
 (0)