visual debugger for jupyter notebooks: myth or reality? · 2019-07-10 · visual debugger for...

68
Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova EuroPython 2019

Upload: others

Post on 23-Jun-2020

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Visual Debugger for Jupyter Notebooks:

Myth or Reality?Elizaveta Shashkova

EuroPython 2019

Page 2: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

About Me

•Software Developer at JetBrains, PyCharm IDE

•Debugger and Data Science tools

• @lisa_shashkova

�2

Page 3: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Visual Debugger

�3

Page 4: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Notebooks

•Popular scientific tool

• File is a sequence of cells

�4

Page 5: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Notebooks Debug

�5

•Logging with print statements

•Command-line debugger ipdb

Page 6: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Notebooks Debug

�6

Page 7: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Myth or Reality?

Page 8: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Myth or Reality?

Page 9: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Contents

•Python files debugging

• Jupyter breakpoints

•Debugger communication

• Jupyter visual debugger

�9

Page 10: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Tracing Function

�10

def tracefunc(frame, event, arg): print(frame.f_lineno, event) return tracefunc

sys.settrace(tracefunc)

1 2 3 4 5 6

Page 11: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Tracing Function

�11

def greet_neighbors(): planets = ["Mars", "Venus"] for p in planets: print(f"Hi {p}!") return len(planets)

sys.settrace(tracefunc) greet_neighbors()

1 2 3 4 5 6 7 8 9

Page 12: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Tracing Function

�12

1 2 3 4 5 6 7 8 9

1 call def greet_neighbors(): planets = ["Mars", "Venus"] for p in planets: print(f"Hi {p}!") return len(planets)

sys.settrace(tracefunc) greet_neighbors()

Page 13: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Tracing Function

�13

1 2 3 4 5 6 7 8 9

1 call 2 line

def greet_neighbors(): planets = ["Mars", "Venus"] for p in planets: print(f"Hi {p}!") return len(planets)

sys.settrace(tracefunc) greet_neighbors()

Page 14: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Tracing Function

�14

1 2 3 4 5 6 7 8 9

1 call 2 line 3 line 4 line Hi Mars!

def greet_neighbors(): planets = ["Mars", "Venus"] for p in planets: print(f"Hi {p}!") return len(planets)

sys.settrace(tracefunc) greet_neighbors()

Page 15: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Tracing Function

�15

1 2 3 4 5 6 7 8 9

1 call 2 line 3 line 4 line Hi Mars! 3 line 4 line Hi Venus!

def greet_neighbors(): planets = ["Mars", "Venus"] for p in planets: print(f"Hi {p}!") return len(planets)

sys.settrace(tracefunc) greet_neighbors()

Page 16: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Tracing Function

�16

1 2 3 4 5 6 7 8 9

def greet_neighbors(): planets = ["Mars", "Venus"] for p in planets: print(f"Hi {p}!") return len(planets)

sys.settrace(tracefunc) greet_neighbors()

1 call 2 line 3 line 4 line Hi Mars! 3 line 4 line Hi Venus! 5 line 5 return

Page 17: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Breakpoint

•frame.f_lineno - current line number

•frame.f_code.co_filename - current file name

�17

Page 18: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Breakpoint

•frame.f_lineno - current line number

•frame.f_code.co_filename - current file name

•Equals to breakpoint’s file and line -> suspend program!

�18

Page 19: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Contents

•Python files debugging

• Jupyter breakpoints

•Debugger communication

• Jupyter visual debugger

�19

Page 20: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Contents

•Python files debugging

• Jupyter breakpoints

•Debugger communication

• Jupyter visual debugger

�20

Page 21: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Cells Execution

�21

IPython kernel

Front-end

IDE

Page 22: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Cells Execution

�22

code IPython kernel

Front-end

IDE

Page 23: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

IPython kernel

Cells Execution

�23

code executionFront-end

IDE

Page 24: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Cells Execution

�24

result

IPython kernel

Front-end

IDE

Page 25: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Cells Execution

•Kernel generates a unique name for each cell

• <ipython-input-5-11faed10a894>

•File name of a generated code object

�25

IPython kernel

code execution

Page 26: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Breakpoints

•Python files: (filename, line number) -> unique location

�26

Page 27: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Breakpoints

•Python files: (filename, line number) -> unique location

• Jupyter Notebooks?

�27

Page 28: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Breakpoints

•Python files: (filename, line number) -> unique location

• Jupyter Notebooks:

•generated cell name

• line inside code object

�28

Page 29: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Source Mapping

�29

IPython kernelIDE

cell source codeMyNotebook.ipynb

generated <code object>

Page 30: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Source Mapping

�30

IPython kernelIDE

cell source codeMyNotebook.ipynb

cell idgenerated

<code object>

Page 31: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

IPython kernelIDE

Source Mapping

�31

?

cell source codeMyNotebook.ipynb

cell idgenerated

<code object>

Page 32: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Source Mapping

�32

•Tracking cells execution in the IDE

Page 33: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Source Mapping

�33

•Tracking cells execution in the IDE

•Silent cell execution in IPython kernel

Page 34: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Debug Cell Execution

�34

<cell source code>

Page 35: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Debug Cell Execution

�35

<cell source code>

patch name generation

- silent mode

Page 36: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Debug Cell Execution

�36

<cell source code>

patch name generation

cell id

- silent mode

Page 37: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Tracing Function

•frame.f_code.co_filename - generated name

�37

Page 38: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Tracing Function

•frame.f_code.co_filename - generated name

•Map: generated name -> cell id

�38

Page 39: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Tracing Function

•frame.f_code.co_filename - generated name

•Map: generated name -> cell id

• Send message to the IDE

�39

Page 40: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Tracing Function

•frame.f_code.co_filename - generated name

•Map: generated name -> cell id

• Send message to the IDE

�40

Page 41: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Contents

•Python files debugging

• Jupyter breakpoints

•Debugger communication

• Jupyter visual debugger

�41

Page 42: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Contents

•Python files debugging

• Jupyter breakpoints

•Debugger communication

• Jupyter visual debugger

�42

Page 43: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Debug Communication

�43

“Add breakpoint in a cell 3, line 2”

IPython kernel

Front-end

IDE

Page 44: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Debug Communication

�44

“Add breakpoint in a cell 3, line 2”

IPython kernel

Front-end

IDE

Page 45: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Debug Communication

�45

•Additional connection

•Reuse Jupyter channels

Page 46: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Messaging

�46

Front-end

IPython kernelKernel proxy

Page 47: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Messaging

�47

Front-end

IPython kernelKernel proxy

Page 48: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Debug Communication

�48

•Additional connection

•Reuse Jupyter channels

Page 49: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Debug Communication

�49

•Additional connection

•Reuse Jupyter channels

Page 50: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Messaging

�50

Front-endKernel proxy

IPython kernelShell

IOPub

stdin

Page 51: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Architecture

�51

•Event loop in a main thread for execution events

•Event loop for output events

Page 52: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Architecture

�52

code

debug

x Blocked

IPython kernel

Front-end

IDE

Page 53: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Debug Communication

�53

•Additional connection

•Reuse Jupyter channels

Page 54: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Debug Communication

�54

•Additional connection

•Reuse Jupyter channels

But ipdb works!

Page 55: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

But ipdb Works!

�55

Page 56: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

But ipdb Works!

�56

•Based on input()

•Reuses user input channel

Page 57: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Debug Communication

�57

•Additional connection

•Reuse Jupyter channels

Page 58: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Contents

•Python files debugging

• Jupyter breakpoints

•Debugger communication

• Jupyter visual debugger

�58

Page 59: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Contents

•Python files debugging

• Jupyter breakpoints

•Debugger communication

• Jupyter visual debugger

�59

Page 60: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Visual Debugger

• Jupyter tracing function

�60

Page 61: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Visual Debugger

• Jupyter tracing function

•Mapping between editor and generated code

�61

Page 62: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Visual Debugger

• Jupyter tracing function

•Mapping between editor and generated code

•Debugger connection

�62

Page 63: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Live Demo

�63

Page 64: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Live Demo

�64

•PyCharm doesn’t convert Jupyter Notebooks to Python files!

•On disk it’s still the same JSON file with .ipynb extension

Page 65: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Visual Debugger

• Jupyter tracing function

•Mapping between editor and generated code

•Debugger connection

�65

Page 66: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Visual Debugger

•Implement in your favourite IDE

�66

Page 67: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Visual Debugger

•Implement in your favourite IDE

•Try it in PyCharm Pro!

�67

Page 68: Visual Debugger for Jupyter Notebooks: Myth or Reality? · 2019-07-10 · Visual Debugger for Jupyter Notebooks: Myth or Reality? Elizaveta Shashkova ... •Python files debugging

Jupyter Visual Debugger

•Implement in your favourite IDE

•Try it in PyCharm Pro!

•Questions?

�68

@lisa_shashkova