Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing source in Jupyter notebook/lab #93

Merged
merged 1 commit into from
Sep 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion line_profiler/line_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import functools
import inspect
import linecache
import tempfile
import os
import sys
from argparse import ArgumentError, ArgumentParser
Expand Down Expand Up @@ -194,6 +195,15 @@ def add_module(self, mod):
return nfuncsadded


def is_ipython_kernel_cell(filename):
""" Return True if a filename corresponds to a Jupyter Notebook cell
"""
return (
filename.startswith("<ipython-input-") or
filename.startswith(tempfile.gettempdir() + '/ipykernel_')
)


def show_func(filename, start_lineno, func_name, timings, unit,
output_unit=None, stream=None, stripzeros=False):
""" Show results for a single function.
Expand All @@ -217,7 +227,7 @@ def show_func(filename, start_lineno, func_name, timings, unit,
scalar = unit / output_unit

stream.write("Total time: %g s\n" % (total_time * unit))
if os.path.exists(filename) or filename.startswith("<ipython-input-"):
if os.path.exists(filename) or is_ipython_kernel_cell(filename):
stream.write("File: %s\n" % filename)
stream.write("Function: %s at line %s\n" % (func_name, start_lineno))
if os.path.exists(filename):
Expand Down