Skip to content

kernel/file-io-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kernel File I/O Demo

A demonstration of file download automation using Kernel cloud browsers with Playwright and Chrome DevTools Protocol (CDP).

This example shows how to automate downloading files from websites using Kernel's remote browser infrastructure, then retrieve those files from the cloud browser's filesystem to your local machine. Kernel File I/O Youtube Video Screenshot

What This Demo Does

This script automates a receipt download workflow:

  1. Creates a remote browser session via Kernel
  2. Navigates to a parking receipt lookup page
  3. Fills out a form with email and order number
  4. Downloads a PDF receipt
  5. Retrieves the file from the remote browser filesystem
  6. Streams it to your local ./downloads/ directory

Architecture

The demo combines three key technologies:

  • Kernel: Provides cloud-hosted browsers accessible via API
  • Playwright: Automates browser interactions (form filling, clicking, navigation)
  • CDP (Chrome DevTools Protocol): Handles file download tracking and retrieval

Key Technical Highlights

File Download Handling via CDP

Unlike standard Playwright download handling, this demo uses CDP to:

  • Set download behavior on the remote browser (Browser.setDownloadBehavior)
  • Track download events (Browser.downloadWillBegin, Browser.downloadProgress)
  • Access files in the remote filesystem via Kernel's file API

Streaming File Transfer

Files are streamed from the remote browser to local disk using client.browsers.fs.read_file() and write_to_file(), avoiding loading large files into memory.

Prerequisites

  • Python 3.11 or higher
  • uv package manager
  • A Kernel API key (sign up at onkernel.com)

Installation

  1. Clone this repository:
git clone <your-repo-url>
cd file-io-demo
  1. Install dependencies using uv:
uv sync
  1. Create a .env file in the project root:
KERNEL_API_KEY=your_api_key_here
OPENAI_API_KEY=your_openai_api_key_here

Usage

Run the automation script:

python main.py

The script will:

  • Print the Kernel browser live view URL (watch the automation in real-time)
  • Navigate through the demo site
  • Download the receipt
  • Save it to ./downloads/

Customizing for Your Use Case

To adapt this for your own website automation:

  1. Update the configuration variables in main.py:

    WEBSITE_URL = "your-website-url"
    EMAIL = "your-email"
    ORDER_NUMBER = "your-order-number"
    DOWNLOAD_DIR = "/tmp/downloads"
  2. Modify the Playwright selectors to match your target website's HTML structure

  3. Adjust the CDP event handlers if needed for different download patterns

Project Structure

  • main.py - Main automation script with download logic
  • session.py - Custom BrowserSession subclass that fixes viewport resizing when connecting via CDP
  • pyproject.toml - Project dependencies and configuration
  • .env - Environment variables (API keys)

Development

Type checking:

mypy .

Learn More

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

File IO demo repository

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages