Skip to content

Commit

Permalink
🐛 Account for cell source being a string (#286)
Browse files Browse the repository at this point in the history
* Account for source being a string

* test

* make string
  • Loading branch information
Koncopd authored Oct 16, 2024
1 parent c28c19b commit aea3d36
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 16 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ repos:
exclude: |
(?x)(
docs/examples/|
docs/notes/
docs/notes/|
cell-source-string.ipynb
)
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7
Expand All @@ -48,8 +49,7 @@ repos:
hooks:
- id: mypy
args: [--no-strict-optional, --ignore-missing-imports]
additional_dependencies:
["types-pkg-resources", "types-requests", "types-attrs"]
additional_dependencies: ["types-requests", "types-attrs"]
exclude: |
(?x)(
test_notebooks.py
Expand Down
2 changes: 2 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Edges cases, warnings, and errors:
- {doc}`faq/internal-functions`
- {doc}`faq/set-env-via-environment-var`
- {doc}`faq/header-author-field`
- {doc}`faq/cell-source-string`

```{toctree}
:maxdepth: 1
Expand All @@ -48,4 +49,5 @@ faq/not-initialized
faq/internal-functions
faq/set-env-via-environment-var
faq/header-author-field
faq/cell-source-string
```
63 changes: 63 additions & 0 deletions docs/faq/cell-source-string.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "65e19088d582d71d",
"metadata": {},
"source": "# Check with cell sources being strings"
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a01a044c3767dc9",
"metadata": {},
"outputs": [],
"source": "import nbproject"
},
{
"cell_type": "code",
"execution_count": null,
"id": "7051321d7eb8c452",
"metadata": {},
"outputs": [],
"source": "assert nbproject.meta.live.title == \"Check with cell sources being strings\""
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2d5e63449430ff8",
"metadata": {},
"outputs": [],
"source": "assert nbproject.meta.live.consecutive_cells"
},
{
"cell_type": "code",
"execution_count": null,
"id": "d32912abaefbdc13",
"metadata": {},
"outputs": [],
"source": ""
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
11 changes: 5 additions & 6 deletions nbproject/dev/_check_last_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ def check_last_cell(nb: Notebook, calling_statement: str) -> bool:
nb: Notebook content.
calling_statement: The statement that calls this function.
"""
last_code_cell = None
last_code_cell_source = None
for cell in nb.cells:
if cell["cell_type"] == "code" and cell["source"] != []:
last_code_cell = cell
cell_source = "".join(cell["source"])
if cell["cell_type"] == "code" and cell_source != "":
last_code_cell_source = cell_source

if last_code_cell is not None and calling_statement in "".join(
last_code_cell["source"]
):
if last_code_cell_source is not None and calling_statement in last_code_cell_source:
return True
else:
return False
7 changes: 3 additions & 4 deletions nbproject/dev/_consecutiveness.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ def check_consecutiveness(nb: Notebook, calling_statement: str = None) -> bool:
ccount = 0 # need to initialize because notebook might note have code cells
# and below, we check if ccount is None
for cell in cells:
if cell["cell_type"] != "code" or cell["source"] == []:
cell_source = "".join(cell["source"])
if cell["cell_type"] != "code" or cell_source == "":
continue

if calling_statement is not None and calling_statement in "".join(
cell["source"]
):
if calling_statement is not None and calling_statement in cell_source:
continue

ccount = cell["execution_count"]
Expand Down
2 changes: 1 addition & 1 deletion nbproject/dev/_meta_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_title(nb: Notebook) -> Optional[str]:
# only consider markdown
if cell["cell_type"] == "markdown":
# grab source
text = cell["source"][0]
text = "".join(cell["source"])
# loop through lines
for line in text.split("\n"):
# if finding a level-1 heading, consider it a title
Expand Down
6 changes: 4 additions & 2 deletions nbproject/dev/_meta_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ def _set_execution_count(calling_statement: str, notebook: Notebook):

prev = 0
for cell in cells:
if cell["cell_type"] != "code" or cell["source"] == []:
cell_source = "".join(cell["source"])

if cell["cell_type"] != "code" or cell_source == "":
continue

if calling_statement in "".join(cell["source"]):
if calling_statement in cell_source:
cell["execution_count"] = prev + 1

ccount = cell["execution_count"]
Expand Down

0 comments on commit aea3d36

Please sign in to comment.