Skip to content

Commit f54c6fb

Browse files
lrcoutoAhdraMeraliQBAhdra Meralistichbury
authored
Add documentation on ipython debugging (#3579)
* Add first draft Signed-off-by: lrcouto <laurarccouto@gmail.com> * Remoe outdated kedro jupyter convert docs Signed-off-by: Ahdra Merali <90615669+AhdraMeraliQB@users.noreply.github.com> * Suggestion: Review edits Signed-off-by: Ahdra Merali <90615669+AhdraMeraliQB@users.noreply.github.com> * Update FAQs Signed-off-by: Ahdra Merali <ahdra.merali@quantumblack.com> * Edit jupyter ipython debug section Signed-off-by: lrcouto <laurarccouto@gmail.com> * Change link to section that does not exist anymore Signed-off-by: L. R. Couto <laurarccouto@gmail.com> * Change link to section that does not exist anymore Signed-off-by: L. R. Couto <laurarccouto@gmail.com> * Change wording and formatting Signed-off-by: lrcouto <laurarccouto@gmail.com> * Lint Signed-off-by: lrcouto <laurarccouto@gmail.com> * Update docs/source/notebooks_and_ipython/kedro_and_notebooks.md Co-authored-by: Jo Stichbury <jo_stichbury@mckinsey.com> Signed-off-by: L. R. Couto <57910428+lrcouto@users.noreply.github.com> * Update docs/source/notebooks_and_ipython/kedro_and_notebooks.md Co-authored-by: Ahdra Merali <90615669+AhdraMeraliQB@users.noreply.github.com> Signed-off-by: L. R. Couto <57910428+lrcouto@users.noreply.github.com> * Changes to the wording, remove unnecessary section Signed-off-by: lrcouto <laurarccouto@gmail.com> * Update docs/source/notebooks_and_ipython/kedro_and_notebooks.md Co-authored-by: Jo Stichbury <jo_stichbury@mckinsey.com> Signed-off-by: L. R. Couto <57910428+lrcouto@users.noreply.github.com> * Update docs/source/notebooks_and_ipython/kedro_and_notebooks.md Co-authored-by: Jo Stichbury <jo_stichbury@mckinsey.com> Signed-off-by: L. R. Couto <57910428+lrcouto@users.noreply.github.com> * Update docs/source/notebooks_and_ipython/kedro_and_notebooks.md Co-authored-by: Jo Stichbury <jo_stichbury@mckinsey.com> Signed-off-by: L. R. Couto <57910428+lrcouto@users.noreply.github.com> * Update docs/source/notebooks_and_ipython/kedro_and_notebooks.md Co-authored-by: Jo Stichbury <jo_stichbury@mckinsey.com> Signed-off-by: L. R. Couto <57910428+lrcouto@users.noreply.github.com> * Update docs/source/notebooks_and_ipython/kedro_and_notebooks.md Co-authored-by: Jo Stichbury <jo_stichbury@mckinsey.com> Signed-off-by: L. R. Couto <57910428+lrcouto@users.noreply.github.com> * Change passive tense on debugging section Signed-off-by: lrcouto <laurarccouto@gmail.com> --------- Signed-off-by: lrcouto <laurarccouto@gmail.com> Signed-off-by: Ahdra Merali <90615669+AhdraMeraliQB@users.noreply.github.com> Signed-off-by: Ahdra Merali <ahdra.merali@quantumblack.com> Signed-off-by: L. R. Couto <laurarccouto@gmail.com> Signed-off-by: L. R. Couto <57910428+lrcouto@users.noreply.github.com> Co-authored-by: Ahdra Merali <90615669+AhdraMeraliQB@users.noreply.github.com> Co-authored-by: Ahdra Merali <ahdra.merali@quantumblack.com> Co-authored-by: Jo Stichbury <jo_stichbury@mckinsey.com>
1 parent 64296cf commit f54c6fb

File tree

4 files changed

+47
-18
lines changed

4 files changed

+47
-18
lines changed

docs/source/faq/faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This is a growing set of technical FAQs. The [product FAQs on the Kedro website]
1414

1515
## Working with Jupyter
1616

17-
* [How can I convert functions from Jupyter Notebooks into Kedro nodes](../notebooks_and_ipython/kedro_and_notebooks.md#how-to-use-tags-to-convert-functions-from-jupyter-notebooks-into-kedro-nodes)?
17+
* [How can I debug a Kedro project in a Jupyter notebook](../notebooks_and_ipython/kedro_and_notebooks.md#debugging-with-debug-and-pdb)?
1818
* [How do I connect a Kedro project kernel to other Jupyter clients like JupyterLab](../notebooks_and_ipython/kedro_and_notebooks.md#ipython-jupyterlab-and-other-jupyter-clients)?
1919

2020
## Kedro project development
Loading
Loading

docs/source/notebooks_and_ipython/kedro_and_notebooks.md

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -209,32 +209,61 @@ You don't need to restart the kernel for the `catalog`, `context`, `pipelines` a
209209

210210
For more details, run `%reload_kedro?`.
211211

212-
## How to use tags to convert functions from Jupyter notebooks into Kedro nodes
212+
## Useful to know (for advanced users)
213+
Each Kedro project has its own Jupyter kernel so you can switch between Kedro projects from a single Jupyter instance by selecting the appropriate kernel.
213214

214-
You can use the notebook to write experimental code for your Kedro project. If you later want to convert functions you've written to Kedro nodes, you can do this using `node` tags to export them to a Python file. Say you have the following code in your notebook:
215+
To ensure that a Jupyter kernel always points to the correct Python executable, if one already exists with the same name `kedro_<package_name>`, then it is replaced.
215216

216-
```ipython
217-
def some_action():
218-
print("This function came from `notebooks/my_notebook.ipynb`")
219-
```
217+
You can use the `jupyter kernelspec` set of commands to manage your Jupyter kernels. For example, to remove a kernel, run `jupyter kernelspec remove <kernel_name>`.
220218

221-
1. Enable tags toolbar: `View` menu -> `Cell Toolbar` -> `Tags`
222-
![Enable the tags toolbar graphic](../meta/images/jupyter_notebook_workflow_activating_tags.png)
219+
### Debugging with %debug and %pdb
223220

224-
2. Add the `node` tag to the cell containing your function
225-
![Add the node tag graphic](../meta/images/jupyter_notebook_workflow_tagging_nodes.png)
221+
You can use the `%debug` [line magic](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-debug) to launch an interactive debugger in your Jupyter notebook. Declare it before a single-line statement to step through the execution in debug mode. You can use the argument `--breakpoint` or `-b` to provide a breakpoint.
222+
The follow sequence occurs when `%debug` runs immediately after an error occurs:
223+
- The stack trace of the last unhandled exception loads.
224+
- The program stops at the point where the exception occurred.
225+
- An interactive shell where the user can navigate through the stack trace opens.
226226

227+
You can then inspect the value of expressions and arguments, or add breakpoints to the code.
227228

228-
3. Save your Jupyter notebook to `notebooks/my_notebook.ipynb`
229-
4. From your terminal, run `kedro jupyter convert notebooks/my_notebook.ipynb` from the Kedro project directory. The output is a Python file `src/<package_name>/nodes/my_notebook.py` containing the `some_action` function definition
230-
5. The `some_action` function can now be used in your Kedro pipelines
229+
<details>
230+
<summary>Click to see an example.</summary>
231231

232-
## Useful to know (for advanced users)
233-
Each Kedro project has its own Jupyter kernel so you can switch between Kedro projects from a single Jupyter instance by selecting the appropriate kernel.
232+
![jupyter_ipython_debug_command](../meta/images/jupyter_ipython_debug_command.gif)
234233

235-
If a Jupyter kernel with the name `kedro_<package_name>` already exists then it is replaced. This ensures that the kernel always points to the correct Python executable. For example, if you change conda environment in a Kedro project then you should re-run `kedro jupyter notebook` to replace the kernel specification with one that points to the new environment.
234+
</details>
235+
236+
---
237+
238+
You can set up the debugger to run automatically when an exception occurs by using the `%pdb` [line magic](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-pdb). This automatic behaviour can be enabled with `%pdb 1` or `%pdb on` before executing a program, and disabled with `%pdb 0` or `%pdb off`.
239+
240+
<details>
241+
<summary>Click to see an example.</summary>
242+
243+
![jupyter_ipython_pdb_command](../meta/images/jupyter_ipython_pdb_command.gif)
244+
245+
</details>
246+
247+
---
248+
249+
Some examples of the possible commands that can be used to interact with the ipdb shell are as follows:
250+
251+
| Command | Description |
252+
| ----------------- | ----------------------------------------------------- |
253+
| `list` | Show the current location in the file |
254+
| `h(elp)` | Show a list of commands, or find help on a specific command |
255+
| `q(uit)` | Quit the debugger and the program |
256+
| `c(ontinue)` | Quit the debugger, continue in the program |
257+
| `n(ext)` | Go to the next step of the program |
258+
| `<enter>` | Repeat the previous command |
259+
| `p(rint)` | Print variables |
260+
| `s(tep)` | Step into a subroutine |
261+
| `r(eturn)` | Return out of a subroutine |
262+
| `b(reak)` | Insert a breakpoint |
263+
| `a(rgs)` | Print the argument list of the current function |
264+
265+
For more information, use the `help` command in the debugger, or take at the [ipdb repository](https://github.com/gotcha/ipdb) for guidance.
236266

237-
You can use the `jupyter kernelspec` set of commands to manage your Jupyter kernels. For example, to remove a kernel, run `jupyter kernelspec remove <kernel_name>`.
238267

239268
### Managed services
240269

0 commit comments

Comments
 (0)