GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-142927: Auto-open HTML output in browser after generation by ivonastojanovic · Pull Request #143178 · python/cpython

gh-142927: Auto-open HTML output in browser after generation #143178

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.

Sign up for GitHub

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

Merged
pablogsal merged 1 commit into python:main from ivonastojanovic:auto_open_browser
Jan 1, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-142927: Auto-open HTML output in browser after generation #143178

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
2d6e994
Add --browser flag to auto-open HTML output after generation
ivonastojanovic Dec 31, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

7 changes: 7 additions & 0 deletions 7 Doc/library/profiling.sampling.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,13 @@ Output options
named ``<format>_<PID>.<ext>`` (for example, ``flamegraph_12345.html``).
:option:`--heatmap` creates a directory named ``heatmap_<PID>``.

.. option:: --browser

Automatically open HTML output (:option:`--flamegraph` and
:option:`--heatmap`) in your default web browser after generation.
When profiling with :option:`--subprocesses`, only the main process
opens the browser; subprocess outputs are never auto-opened.


pstats display options
----------------------
Expand Down
41 changes: 41 additions & 0 deletions 41 Lib/profiling/sampling/cli.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import subprocess
import sys
import time
import webbrowser
from contextlib import nullcontext

from .errors import SamplingUnknownProcessError, SamplingModuleNotFoundError, SamplingScriptNotFoundError
Expand Down Expand Up @@ -492,6 +493,12 @@ def _add_format_options(parser, include_compression=True, include_binary=True):
help="Output path (default: stdout for pstats, auto-generated for others). "
"For heatmap: directory name (default: heatmap_PID)",
)
output_group.add_argument(
"--browser",
action="store_true",
help="Automatically open HTML output (flamegraph, heatmap) in browser. "
"When using --subprocesses, only the main process opens the browser",
)


def _add_pstats_options(parser):
Expand Down Expand Up @@ -591,6 +598,32 @@ def _generate_output_filename(format_type, pid):
return f"{format_type}_{pid}.{extension}"


def _open_in_browser(path):
"""Open a file or directory in the default web browser.

Args:
path: File path or directory path to open

For directories (heatmap), opens the index.html file inside.
"""
abs_path = os.path.abspath(path)

# For heatmap directories, open the index.html file
if os.path.isdir(abs_path):
index_path = os.path.join(abs_path, 'index.html')
if os.path.exists(index_path):
abs_path = index_path
else:
print(f"Warning: Could not find index.html in {path}", file=sys.stderr)
return

file_url = f"file://{abs_path}"
try:
webbrowser.open(file_url)
except Exception as e:
print(f"Warning: Could not open browser: {e}", file=sys.stderr)


def _handle_output(collector, args, pid, mode):
"""Handle output for the collector based on format and arguments.

Expand Down Expand Up @@ -630,6 +663,10 @@ def _handle_output(collector, args, pid, mode):
filename = args.outfile or _generate_output_filename(args.format, pid)
collector.export(filename)

# Auto-open browser for HTML output if --browser flag is set
if args.format in ('flamegraph', 'heatmap') and getattr(args, 'browser', False):
_open_in_browser(filename)


def _validate_args(args, parser):
"""Validate format-specific options and live mode requirements.
Expand Down Expand Up @@ -1153,6 +1190,10 @@ def progress_callback(current, total):
filename = args.outfile or _generate_output_filename(args.format, os.getpid())
collector.export(filename)

# Auto-open browser for HTML output if --browser flag is set
if args.format in ('flamegraph', 'heatmap') and getattr(args, 'browser', False):
_open_in_browser(filename)

print(f"Replayed {count} samples")


Expand Down
5 changes: 5 additions & 0 deletions 5 Lib/test/test_profiling/test_sampling_profiler/test_children.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ def assert_flag_value_pair(flag, value):
child_args,
f"Flag '--flamegraph' not found in args: {child_args}",
)
self.assertNotIn(
"--browser",
child_args,
f"Flag '--browser' should not be in child args: {child_args}",
)

def test_build_child_profiler_args_no_gc(self):
"""Test building CLI args with --no-gc."""
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.