question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: magic command to use viztracer from within jupyter notebook

See original GitHub issue

Note sure what would be the best way to implement this, but the usability of viztracer could be greatly improved for datascientists and other Python users who work in interactive envrionments based on Jupyter such as Jupyter Lab and VS Code notebooks.

The setup use case would work as follow in jupyter notebook with the following cells:

# Notebook cell to enable the viztracer <=> jupyter extension
%load_ext viztracer
# Here is a notebook 
biggish_data = prepare_big_datastructure_or_load_from_disk()
# Here is a notebook cell with some code to interactively
# define some complex data processing function.

def some_function(some_data, some_param=0.):
     # do complicated stuff
     ...
# Here is a cell that profiles on
%%viztracer
some_function(biggish_data, some_param=42)

The last cell would execute some_function(biggish_data, some_param=42) while recording the profiling events to a temp file on the disk and then automatically display an iframe with the perfetto viewer of the recorded events right below the code cell with the %%viztracer statement.

If instead one uses %%viztracer --new-tab would open a new browser tab to the viewer instead of using an iframe.

Finally %%viztracer --static-html would embed the legacy static HTML viewer in the output of the cell of the notebok document to make it easier to publish and share the notebook (e.g. on https://nbviewer.jupyter.org) so that other can browse the profiling output without having to run a (local or remote) server (for jupyter and vizviewer).

It would also be possible to pass any viztracer commandline options to the magic command itself, for instance %%viztracer --log_gc --log_subprocess.

This setup is really nice, because prepare_big_datastructure_or_load_from_disk can be very slow and the user might want to iterate quickly by editing the code of some_function in the previous cell based on the result of the previous profiling output without calling prepare_big_datastructure_or_load_from_disk() again each time.

The last 2 cells could be even be collapsed into one for even faster coding iterations:

# Here is a notebook cell with some code to interactively
# define some complex data processing function and profile
# it on the fly:

def some_function(some_data, some_param=0.):
     # do complicated stuff
     ...

%viztracer some_function(biggish_data, some_param=42)

Related jupyter extensions for profilers that can be used for inspiration:

  • jupyter supports the %%prun profiler magic based on cProfile from the standard library by default.
  • line_profiler can register the %%lprun magic command.
  • snakeviz can register the %%snakeviz magic command. It has the --new-tab option I mentioned before.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:23 (21 by maintainers)

github_iconTop GitHub Comments

2reactions
gaogaotiantiancommented, Aug 18, 2021

Hmm, how large are we talking about? It’s possible that Firefox has a different resource limit than Chrome which lead to this issue. If that’s the case, then there’s probably nothing I can do. Personally I use Chrome as my default browser and for the default trace entries I have never met a case where it fails to load. I’m sorry that this causes inconvenience for you, but it seems like at this point there’s not much I can do on viztracer side to solve the issue.

1reaction
laundmocommented, Aug 30, 2021

I found a workaround, after getting the same RPC framing error:

You can load the json in https://ui.perfetto.dev/ and it works just fine.

So, this does not seem like a issue with Perfetto, rather with a interaction between Perfetto and vizviewer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Built-in magic commands — IPython 8.7.0 documentation
You can create and register your own Magics with IPython. You can find many user defined Magics on PyPI. Feel free to publish...
Read more >
Top 8 magic commands in Jupyter Notebook
Magic commands are special commands that can help you with running and analyzing data in your notebook. They add a special functionality ......
Read more >
Top 12 Magic Commands in Jupyter - Medium
In this post, we will learn some of the more popular and useful magic commands for your Jupyter notebook. The list is by...
Read more >
Python debugging, profiling and testing - Dan MacKinlay
VS Code debugger; pudb; PyCharm; Viztracer; pysnooper; Pyrasite ... Any time I find myself needing to debug debugging in jupyter I am ...
Read more >
Python | Everything I know - My Knowledge Wiki
Pyright - Static type checker for the Python language. cpython - Python programming language source code. PySnooper - Never use print for debugging...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found