Skip to main content

nokap

Screenshots and PDFs from web pages. Powered by headless Chrome.

Python Versions PyPI License CI

nokap captures screenshots and PDFs from web pages (or local HTML) using headless Chrome via the Chrome DevTools Protocol. It doesn't need Selenium or Playwright. Its only runtime dependencies are websockets and click.

Installation

pip install nokap

nokap uses the Chrome or Chromium already on your system and will hunt it down pretty quickly. If you don't have one, or you're running inside a sandbox where full Chrome can't start (such as a coding agent's sandbox on macOS), install Chrome's standalone headless build:

nokap install-chrome

This downloads chrome-headless-shell from Chrome for Testing into nokap's cache, and nokap uses it from then on. Nothing is downloaded unless you run this command. Check your setup with nokap doctor.

Quick Start

import nokap

# Screenshot a URL
nokap.webshot("https://example.com", "example.png")

# Save as PDF
nokap.webshot("https://example.com", "example.pdf")

# Screenshot with a CSS selector (captures just that element)
nokap.webshot("https://example.com", "header.png", selector="h1")

# From an HTML string (great for table libraries)
nokap.from_html("<h1>Hello, world!</h1>", "hello.png")

API

nokap.webshot()

Take a screenshot or PDF of a web page.

nokap.webshot(
    url,                # URL or local file path
    file="webshot.png", # Output path (.png, .jpg, .webp, .pdf)
    *,
    vwidth=992,         # Viewport width (px)
    vheight=744,        # Viewport height (px)
    selector=None,      # CSS selector to capture
    cliprect=None,      # Clip rectangle (x, y, width, height)
    expand=0,           # Padding around selector (px)
    delay=0.2,          # Wait after page load (seconds)
    zoom=1,             # Scale factor (2 = retina)
    useragent=None,     # Custom User-Agent string
    renderer=None,      # Use an existing Renderer
)

nokap.from_html()

Render an HTML string to an image or PDF. Designed for integration with table/report libraries.

nokap.from_html(
    html,               # HTML content
    file="webshot.png", # Output path
    *,
    selector="html",    # CSS selector to capture
    renderer=None,      # Use an existing Renderer
    **kwargs,           # All webshot() options
)

nokap.Renderer()

Own a browser and its CDP connection explicitly. Construction starts Chrome and connects immediately:

with nokap.Renderer(extra_args=["--lang=de"]) as renderer:
    renderer.webshot("https://example.com", "example.png")
    nokap.from_html("<h1>Hello</h1>", "hello.png", renderer=renderer)
    print(renderer.browser.transport)

The constructor is Renderer(path=None, *, headless=True, extra_args=None, timeout=20, transport=None). Its webshot() and from_html() methods accept the same capture options as the module functions. renderer.browser exposes the underlying Chrome, including its pid and transport.

Reuse one renderer for a batch. Use separate instances for independent tasks or browser settings. Each browser gets its own temporary profile by default. Passing renderer= to a module function borrows the instance; the caller remains responsible for closing it.

Leaving the with block closes the browser and connection. You can also call renderer.close() explicitly; repeated calls are safe. Closing is final: captures on a closed renderer or one whose browser has exited raise an error. Construct a new renderer to continue.

nokap.close()

Close the shared default renderer used by module calls without renderer=. Explicitly constructed renderers are unaffected. The next convenience call creates a new default renderer; it also replaces a default whose browser has exited. Cleanup runs automatically at process exit.

nokap.close()

nokap.install_chrome()

Download chrome-headless-shell into nokap's cache and return its path. Same as nokap install-chrome on the command line.

nokap.install_chrome(
    version="stable",   # Channel (stable/beta/dev/canary), milestone ("156"), or exact version
    *,
    force=False,        # Reinstall even if already present
)

How It Works

nokap communicates directly with Chrome over the Chrome DevTools Protocol, using pipes by default on macOS and Linux and WebSockets on Windows. No browser driver binaries, no heavyweight automation frameworks.

The architecture:

  1. Launch: Finds and starts headless Chrome with pipes or a debugging port
  2. Connect: Opens the connection to Chrome's CDP endpoint
  3. Capture: Creates a tab, navigates, waits, then calls Page.captureScreenshot or Page.printToPDF
  4. Cleanup: Closes the tab; Chrome stays running for reuse until its renderer is closed or the process exits

Features

Feature Details
Image formats PNG, JPEG, WebP
PDF generation Configurable page size, margins, orientation
CSS selectors Capture specific elements (or union of multiple)
Zoom/scale Produce retina-quality (2×, 3×) images
Expand/padding Add whitespace around captured elements
Local HTML Render HTML strings or local .html files
Viewport control Set width/height for responsive layouts
Custom User-Agent Override the browser UA string
Auto-cleanup Chrome process managed via atexit
Jupyter-safe Works in notebooks (no event loop conflicts)
Sandbox-friendly Runs in macOS agent sandboxes with chrome-headless-shell

Browser Discovery

nokap uses the first browser it finds, in this order:

  1. CHROME_PATH
  2. chrome-headless-shell installed by nokap install-chrome
  3. chrome-headless-shell on PATH
  4. Chrome, Chromium, or another Chromium-based browser (such as Edge) in its standard install location

Configuration

Environment Variable Purpose
CHROME_PATH Override Chrome binary location
NOKAP_TRANSPORT Override CDP transport: pipe (default on macOS/Linux) or websocket (default on Windows)
NOKAP_CHROME_ARGS Extra Chrome command-line flags for every launch (shell-style quoting)
NOKAP_CACHE_DIR Where nokap install-chrome stores browsers

To use WebSockets explicitly on macOS or Linux:

NOKAP_TRANSPORT=websocket python render.py

In pipe mode, nokap starts Chrome with --remote-debugging-pipe. CDP commands and responses use inherited file descriptors, so nokap does not open a listening TCP port or connect to one. This also applies to libraries that call nokap, including Great Tables' GT.gtsave(). Both transports use a separate temporary profile for each browser unless --user-data-dir is supplied explicitly.

The sandbox must still allow the selected browser to start and write temporary files. For MCP Console on macOS, install chrome-headless-shell with nokap install-chrome outside the sandbox. nokap uses pipes automatically and selects --single-process if the required macOS user-directory lookups fail with an I/O error. It also retries with --single-process when Chrome reports a Mach service registration denial. See the MCP Console example for returning a Great Tables table as an MCP image without changing sandbox permissions.

Configuration is read once when a renderer is constructed. Later environment changes do not reconfigure that instance. Construct another renderer for different settings, or call nokap.close() to reset the shared default. For Renderer and Chrome, explicit transport= overrides NOKAP_TRANSPORT; extra_args= are appended after NOKAP_CHROME_ARGS.

Chrome.port and Chrome.ws_url require WebSocket transport. Use Renderer(transport="websocket") and access them through renderer.browser, or construct Chrome(transport="websocket") directly. Pipe transport requires POSIX and is not supported on Windows.

Requirements

  • Python ≥ 3.10
  • Chrome or Chromium installed on the system, or chrome-headless-shell installed with nokap install-chrome

License

MIT

Metadata

Release files for nokap 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nokap 0.2.0
File Size Uploaded Provenance Actions
nokap-0.2.0.tar.gz 110.4 kB Without provenance Details for nokap-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nokap 0.2.0
File Interpreter ABI Platform Actions
nokap-0.2.0-py3-none-any.whl Python 3 none any Details for nokap-0.2.0-py3-none-any.whl

Total release size: 159.0 kB

Release files / nokap-0.2.0.tar.gz

Download URL nokap-0.2.0.tar.gz
Size 110.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c98bff7129981e86268f7b32f6371a3805924f686035a0d035227b3d194a7227
BLAKE2b-256 checksum
How to use checksums
e63b775132d25cd3d3dc1590f702a298ffc5291e381ef92c38086d081a977861
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / nokap-0.2.0-py3-none-any.whl

Download URL nokap-0.2.0-py3-none-any.whl
Size 48.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c5e7fa89c2c4c8f68b75261f3d49eadb0c63526dad04925adcd7aa3621036c19
BLAKE2b-256 checksum
How to use checksums
983eae0533a425082d646f238a4f06995f178aaf80b1927fffb081f29b9877af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for nokap 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page