Skip to content

Commit

Permalink
Use ruff on notebooks and update typings (jupyter#2068)
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Nov 8, 2023
1 parent d10418d commit 6b8c32c
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 169 deletions.
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ repos:
"nbformat",
"markupsafe",
"mistune",
"nbclient",
"nbclient>=0.9",
"defusedxml",
"ipython",
"packaging",
Expand All @@ -84,8 +84,10 @@ repos:
rev: v0.1.4
hooks:
- id: ruff
types_or: [python, jupyter]
args: ["--fix", "--show-fixes"]
- id: ruff-format
types_or: [python, jupyter]

- repo: https://github.com/scientific-python/cookie
rev: "2023.10.27"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,7 @@ raw template
{%- endblock in_prompt -%}
"""


exporter_attr = AttrExporter()
output_attr, _ = exporter_attr.from_notebook_node(nb)
assert "raw template" in output_attr
Expand Down
37 changes: 19 additions & 18 deletions docs/source/nbconvert_library.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
"source": [
"from urllib.request import urlopen\n",
"\n",
"url = 'https://jakevdp.github.io/downloads/notebooks/XKCD_plots.ipynb'\n",
"url = \"https://jakevdp.github.io/downloads/notebooks/XKCD_plots.ipynb\"\n",
"response = urlopen(url).read().decode()\n",
"response[0:60] + ' ...'"
"response[0:60] + \" ...\""
]
},
{
Expand Down Expand Up @@ -102,7 +102,7 @@
"\n",
"# 2. Instantiate the exporter. We use the `classic` template for now; we'll get into more details\n",
"# later about how to customize the exporter further.\n",
"html_exporter = HTMLExporter(template_name='classic')\n",
"html_exporter = HTMLExporter(template_name=\"classic\")\n",
"\n",
"# 3. Process the notebook we loaded earlier\n",
"(body, resources) = html_exporter.from_notebook_node(jake_notebook)"
Expand All @@ -125,7 +125,7 @@
},
"outputs": [],
"source": [
"print(body[:400] + '...')"
"print(body[:400] + \"...\")"
]
},
{
Expand All @@ -148,9 +148,9 @@
"outputs": [],
"source": [
"print(\"Resources:\", resources.keys())\n",
"print(\"Metadata:\", resources['metadata'].keys())\n",
"print(\"Inlining:\", resources['inlining'].keys())\n",
"print(\"Extension:\", resources['output_extension'])"
"print(\"Metadata:\", resources[\"metadata\"].keys())\n",
"print(\"Inlining:\", resources[\"inlining\"].keys())\n",
"print(\"Extension:\", resources[\"output_extension\"])"
]
},
{
Expand Down Expand Up @@ -192,9 +192,9 @@
"# Convert the notebook to RST format\n",
"(body, resources) = rst_exporter.from_notebook_node(jake_notebook)\n",
"\n",
"print(body[:970] + '...')\n",
"print('[.....]')\n",
"print(body[800:1200] + '...')"
"print(body[:970] + \"...\")\n",
"print(\"[.....]\")\n",
"print(body[800:1200] + \"...\")"
]
},
{
Expand All @@ -218,7 +218,7 @@
},
"outputs": [],
"source": [
"sorted(resources['outputs'].keys())"
"sorted(resources[\"outputs\"].keys())"
]
},
{
Expand All @@ -240,7 +240,7 @@
"source": [
"from IPython.display import Image\n",
"\n",
"Image(data=resources['outputs']['output_3_0.png'], format='png')"
"Image(data=resources[\"outputs\"][\"output_3_0.png\"], format=\"png\")"
]
},
{
Expand Down Expand Up @@ -302,7 +302,7 @@
"from traitlets.config import Config\n",
"\n",
"c = Config()\n",
"c.HTMLExporter.preprocessors = ['nbconvert.preprocessors.ExtractOutputPreprocessor']\n",
"c.HTMLExporter.preprocessors = [\"nbconvert.preprocessors.ExtractOutputPreprocessor\"]\n",
"\n",
"# create the new exporter using the custom config\n",
"html_exporter_with_figs = HTMLExporter(config=c)\n",
Expand Down Expand Up @@ -336,7 +336,7 @@
"print(sorted(resources_with_fig.keys()))\n",
"\n",
"print(\"\\nthe actual figures are:\")\n",
"print(sorted(resources_with_fig['outputs'].keys()))"
"print(sorted(resources_with_fig[\"outputs\"].keys()))"
]
},
{
Expand Down Expand Up @@ -382,6 +382,7 @@
"outputs": [],
"source": [
"from traitlets import Integer\n",
"\n",
"from nbconvert.preprocessors import Preprocessor\n",
"\n",
"\n",
Expand Down Expand Up @@ -450,8 +451,8 @@
"\n",
"dl = DictLoader(\n",
" {\n",
" 'footer': \"\"\"\n",
"{%- extends 'lab/index.html.j2' -%} \n",
" \"footer\": \"\"\"\n",
"{%- extends 'lab/index.html.j2' -%}\n",
"\n",
"{% block footer %}\n",
"FOOOOOOOOTEEEEER\n",
Expand All @@ -461,9 +462,9 @@
")\n",
"\n",
"\n",
"exportHTML = HTMLExporter(extra_loaders=[dl], template_file='footer')\n",
"exportHTML = HTMLExporter(extra_loaders=[dl], template_file=\"footer\")\n",
"(body, resources) = exportHTML.from_notebook_node(jake_notebook)\n",
"for l in body.split('\\n')[-4:]:\n",
"for l in body.split(\"\\n\")[-4:]:\n",
" print(l)"
]
},
Expand Down
2 changes: 1 addition & 1 deletion nbconvert/preprocessors/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def executenb(*args, **kwargs):
# We inherit from both classes to allow for traitlets to resolve as they did pre-6.0.
# This unfortunately makes for some ugliness around initialization as NotebookClient
# assumes it's a constructed class with a nb object that we have to hack around.
class ExecutePreprocessor(Preprocessor, NotebookClient): # type:ignore[misc]
class ExecutePreprocessor(Preprocessor, NotebookClient):
"""
Executes all the cells in a notebook
"""
Expand Down
10 changes: 9 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ nowarn = "test -W default {args}"
detached = true
dependencies = ["pre-commit"]
[tool.hatch.envs.lint.scripts]
build = "pre-commit run --all-files ruff"
build = [
"pre-commit run --all-files ruff",
"pre-commit run --all-files ruff-format",
]

[tool.hatch.envs.typing]
dependencies = [ "pre-commit"]
Expand Down Expand Up @@ -224,6 +227,11 @@ unfixable = [
# TID252 Relative imports from parent modules are banned
# PLR2004 Magic value used in comparison
"tests/*" = ["B011", "F841", "S101", "TID252", "PLR2004"]
"tests/*/*.ipynb" = [
"EM", "B018", "E402", "F405", "SIM", "F403", "F821",
"RUF100", "ICN001", "S605", "S607"
]
"docs/*/*.ipynb" = ["S310", "EM", "B018", "E402", "F405", "SIM", "F811", "N816","E741"]
# F401 'foo' imported but unused
# F403 'x' used; unable to detect undefined names
"nbconvert/*/__init__.py" = ["F401", "F403"]
Expand Down
Loading

0 comments on commit 6b8c32c

Please sign in to comment.