CEF Python Documentation

repository·master·Indexed 25 days ago

https://github.com/cztomczak/cefpython

Python bindings for the Chromium Embedded Framework (CEF), enabling the embedding of an HTML5 rendering engine into Python applications. It can be used as a GUI replacement, a web browser widget for frameworks like Qt and GTK, or for web scraping and automated testing. The API includes the cefpython module for lifecycle management, the Browser object for instance control, and various client handlers (LoadHandler, RequestHandler, etc.) for intercepting browser events.

Tokens
39.7K
Snippets
35
Records
318
Agent score
84%

What's inside CEF Python

  1. Overview of cefpython module functions

    master

    The cefpython module provides the core functions for managing the CEF (Chromium Embedded Framework) lifecycle, browser instances, and global callbacks. Key functional areas include:

    • Lifecycle Management: Initialize, Shutdown, MessageLoop, and QuitMessageLoop.
    • Browser Management: CreateBrowser, CreateBrowserSync, GetBrowserByIdentifier, and GetBrowserByWindowHandle.
    • Global Callbacks: SetGlobalClientCallback, GetGlobalClientCallback, and SetGlobalClientHandler.
    • System & Environment: GetAppPath, GetAppSetting, GetCommandLineSwitch, and GetVersion.
    • Task Scheduling: PostTask and PostDelayedTask.
  2. Understand default download behavior in CEF Python

    master

    By default, CEF 3 handles downloads automatically if ApplicationSettings.downloads_enabled is set to True (which is the default setting). When a download is triggered, the operating system's default SaveAs file dialog is displayed to the user.

    If a user aborts the download via the dialog, LoadHandler.OnLoadError will be triggered with the error code ERR_ABORTED.

  3. Browse CEF Python API by category

    master

    The CEF Python API is organized into several functional categories. You can explore the API surface through the following groups:

    • Modules: Core functionality provided by the cefpython module.
    • Settings: Configuration dictionaries including ApplicationSettings, BrowserSettings, and CommandLineSwitches.
    • Classes and objects: Core entities such as Browser, CookieManager, JavascriptBindings, Request, and WindowInfo.
    • Client handlers (interfaces): Interfaces used to intercept and handle browser events, such as LoadHandler, RequestHandler, DisplayHandler, and KeyboardHandler.
    • Other interfaces: Specialized interfaces like CookieVisitor and WebRequestClient.
  4. Configure CEF framework location on Mac

    master

    When distributing Mac apps, CEF expects the framework at Contents/Frameworks/Chromium Embedded Framework.framework in the top-level app bundle. If your structure differs, set ApplicationSettings.framework_dir_path before calling cef.Initialize().

    If you change the directory structure, you may need to update embedded paths in the libraries using install_name_tool:

    • install_name_tool -rpath old new
    • install_name_tool -change old new
    • install_name_tool -id name

    Verify changes with otool -l file or otool -L file.

  5. Handle Certificate Errors

    master

    When implementing RequestHandler.OnCertificateError(), you may encounter several certificate-related error constants. Common ones include:

    • ERR_CERT_AUTHORITY_INVALID: The certificate is signed by an untrusted authority (e.g., self-signed).
    • ERR_CERT_COMMON_NAME_INVALID: The certificate's common name does not match the hostname.
    • ERR_CERT_DATE_INVALID: The certificate is expired or not yet valid.
    • ERR_CERT_INVALID: The certificate is invalid (non-recoverable).
    • ERR_CERT_REVOKED: The certificate has been revoked.
    • ERR_CERT_UNABLE_TO_CHECK_REVOCATION: Revocation information is unavailable.
    • ERR_SSL_PROTOCOL_ERROR: An SSL protocol error occurred.
    • ERR_SSL_VERSION_OR_CIPHER_MISMATCH: Client and server do not share a common SSL version or cipher suite.
  6. Follow code style guidelines for CEF Python

    master

    When contributing code, adhere to the following standards:

    • PEP 8: Comply with the PEP 8 style guide.
    • Docstrings: Follow PEP 257 conventions.
    • Line Length: Limit all lines to a maximum of 79 characters. Comments should be shorter (max 60-65 characters).
    • Indentation: Use 4 spaces.
    • Newlines: Use Unix-style newlines (\n).
    • Consistency: Ensure new code is consistent with the existing codebase.
  7. Explore CEF Python examples and tutorials

    master

    CEF Python provides various resources to help you get started, including tutorials, full examples, code snippets, and guidance on using PyInstaller for packaging:

    • Tutorial: Detailed step-by-step guide.
    • All examples: A collection of comprehensive usage examples.
    • Snippets: Small, focused code snippets.
    • PyInstaller packager: Instructions for bundling your application.
  8. Implement custom download progress tracking

    master

    CEF Python does not currently provide built-in download progress updates. To implement progress tracking, you must create a custom download implementation by investigating and utilizing callbacks from client handlers. Specifically, consider using:

    • RequestHandler.OnBeforeBrowse
    • RequestHandler.OnBeforeResourceLoad
    • ResourceHandler
  9. Run the Hello World example

    master

    To verify your installation and run the basic hello_world.py example, install the cefpython3 package, clone the repository, and execute the script from the examples/ directory.

    pip install cefpython3==66.0
    git clone https://github.com/cztomczak/cefpython.git
    cd cefpython/examples/
    python hello_world.py