1414from IPython .display import IFrame , display
1515
1616from pyinstrument import Profiler , renderers
17+ from pyinstrument .__main__ import compute_render_options
1718from pyinstrument .frame import Frame
1819from pyinstrument .frame_ops import delete_frame_from_tree
1920from pyinstrument .processors import ProcessorOptions
21+ from pyinstrument .renderers .console import ConsoleRenderer
22+ from pyinstrument .renderers .html import HTMLRenderer
2023
2124_active_profiler = None
2225
@@ -76,6 +79,43 @@ def recreate_transformer(self, target_description: str):
7679 )
7780
7881 @magic_arguments ()
82+ @argument (
83+ "-p" ,
84+ "--render-option" ,
85+ dest = "render_options" ,
86+ action = "append" ,
87+ metavar = "RENDER_OPTION" ,
88+ type = str ,
89+ help = (
90+ "options to pass to the renderer, in the format 'flag_name' or 'option_name=option_value'. "
91+ "For example, to set the option 'time', pass '-p time=percent_of_total'. To pass multiple "
92+ "options, use the -p option multiple times. You can set processor options using dot-syntax, "
93+ "like '-p processor_options.filter_threshold=0'. option_value is parsed as a JSON value or "
94+ "a string."
95+ ),
96+ )
97+ @argument (
98+ "--show-regex" ,
99+ dest = "show_regex" ,
100+ action = "store" ,
101+ metavar = "REGEX" ,
102+ help = (
103+ "regex matching the file paths whose frames to always show. "
104+ "Useful if --show doesn't give enough control."
105+ ),
106+ )
107+ @argument (
108+ "--show" ,
109+ dest = "show_fnmatch" ,
110+ action = "store" ,
111+ metavar = "EXPR" ,
112+ help = (
113+ "glob-style pattern matching the file paths whose frames to "
114+ "show, regardless of --hide or --hide-regex. For example, use "
115+ "--show '*/<library>/*' to show frames within a library that "
116+ "would otherwise be hidden."
117+ ),
118+ )
79119 @argument (
80120 "--interval" ,
81121 type = float ,
@@ -110,6 +150,26 @@ def recreate_transformer(self, target_description: str):
110150 nargs = "*" ,
111151 help = "When used as a line magic, the code to profile" ,
112152 )
153+ @argument (
154+ "--hide" ,
155+ dest = "hide_fnmatch" ,
156+ action = "store" ,
157+ metavar = "EXPR" ,
158+ help = (
159+ "glob-style pattern matching the file paths whose frames to hide. Defaults to "
160+ "hiding non-application code"
161+ ),
162+ )
163+ @argument (
164+ "--hide-regex" ,
165+ dest = "hide_regex" ,
166+ action = "store" ,
167+ metavar = "REGEX" ,
168+ help = (
169+ "regex matching the file paths whose frames to hide. Useful if --hide doesn't give "
170+ "enough control."
171+ ),
172+ )
113173 @no_var_expand
114174 @line_cell_magic
115175 def pyinstrument (self , line , cell = None ):
@@ -126,6 +186,12 @@ def pyinstrument(self, line, cell=None):
126186 """
127187 global _active_profiler
128188 args = parse_argstring (self .pyinstrument , line )
189+
190+ # 2024, always override this for now in IPython,
191+ # we can make an option later if necessary
192+ args .unicode = True
193+ args .color = True
194+
129195 ip = get_ipython ()
130196
131197 if not ip :
@@ -175,10 +241,15 @@ def pyinstrument(self, line, cell=None):
175241 )
176242 return
177243
178- html_renderer = renderers .HTMLRenderer (
179- show_all = args .show_all ,
180- timeline = args .timeline ,
244+ html_config = compute_render_options (
245+ args , renderer_class = HTMLRenderer , unicode_support = True , color_support = True
181246 )
247+
248+ text_config = compute_render_options (
249+ args , renderer_class = HTMLRenderer , unicode_support = True , color_support = True
250+ )
251+
252+ html_renderer = renderers .HTMLRenderer (show_all = args .show_all , timeline = args .timeline )
182253 html_renderer .preprocessors .append (strip_ipython_frames_processor )
183254 html_str = _active_profiler .output (html_renderer )
184255 as_iframe = IFrame (
@@ -188,10 +259,7 @@ def pyinstrument(self, line, cell=None):
188259 extras = ['style="resize: vertical"' , f'srcdoc="{ html .escape (html_str )} "' ],
189260 )
190261
191- text_renderer = renderers .ConsoleRenderer (
192- timeline = args .timeline ,
193- show_all = args .show_all ,
194- )
262+ text_renderer = renderers .ConsoleRenderer (** text_config )
195263 text_renderer .processors .append (strip_ipython_frames_processor )
196264
197265 as_text = _active_profiler .output (text_renderer )
0 commit comments