Python in the browser

Enhancing the Scientific Python ecosystem's interoperability with Pyodide

The why

  • Python projects and their environments are tricky to install and manage
  • For maintainers and core developers: difficult to target various platforms and architectures
  • Slightly steep learning curve a euphemism for "learn it yourself like we all did"
  • Lower barrier to entry to learning Python, especially for new programmers

XKCD 1987

  • Your local computer
  • your IDE/text editor/etc.
  • Online
  • Dev Containers
  • Literate programming/notebook environments (Google Colab, Binder, Deepnote, etc.)

 

The why

Environments where you can run Python

  • Documentation for scientific software needs to do more
  • Some guidelines
    • Explanatory (descriptive)
    • Illustrative (graphs, diagrams, visual aids, and more)
    • Interactive (a boost to user experience)
https://diataxis.fr/

The why

The why

  • In come Pyodide and WebAssembly
    • CPython compiled via the Emscripten toolchain using LLVM to run in the browser
    • A paradigm shift towards code that is written once, and runs everywhere
      • Chromium (Google Chrome, Microsoft Edge, Brave Browser, etc.), Mozilla Firefox, Apple Safari, and even inside browsers on Android and iOS (and iPadOS too)!
    • JupyterLite provides a runtime just like standard Jupyter Notebooks

Literate programming environments

  • Google Colab
  • Binder
  • Deepnote

Interactive programming

  • Real-time code execution
  • Immediate feedback
  • Great for learning and experimentation

Pair programming in the browser

  • Collaborative coding
  • Shared environment
  • Ideal for remote work and education

Limitations of Pyodide

  • No threading or multiprocessing
  • Some modules from Python standard library are unavailable
  • Browser security restrictions – the browser is the most secure piece of software on your system, overall!

Browser-based limitations

  • Sandboxed environment
  • Limited access to system resources
  • Restricted file system access (Emscripten FS, etc.)

Interactive Demo

[Placeholder for chosen demo, unsure what to add here]

  • Sudoku implementation using pygame-ce
  • JupyterLite notebook-based game
  • Treasure hunt with Matplotlib plots
  • Interactive data analysis pipeline

32-bit architecture

  • Pyodide uses 32-bit compilation
  • Impacts floating-point precision
  • Affects compatibility with some libraries (off-by-one errors, etc.)

Pyodide's ABI

  • Application Binary Interface
  • Defines how compiled code interacts with the system
  • Crucial for compatibility and stability

Emscripten Version Consistency

  • Pyodide maintains a consistent Emscripten version per release
  • Ensures  ABI compatibility across releases
  • Balances stability and new features

The future of the Pyodide build system

  • Pyodide will approach a v1 release
  • Expanding possibilities for package integration (build farms such as conda-forge)

Interactive documentation

  • Run code examples directly in docs
  • Test API functionality in real-time
  • Improve learning experience for users

The NumPy docs are now interactive

Enhanced Package Examples

  • Live, editable code snippets
  • Demonstrate real-world use cases
  • Allow users to experiment with parameters

Interactive Tutorials

  • Step-by-step guides with live code
  • Immediate feedback for learners
  • Customizable learning paths

Notebooks in the documentation

  • Jupyter-style notebooks embedded in docs
  • Combine explanations with executable code
  • Great for data science and analysis workflows

Improving overall documentation

  • Clearer, more engaging content
  • Reduced barrier to entry for new users
  • Better understanding of package capabilities

JupyterLite kernels

  • Browser-based Jupyter environments
  • Run notebooks without a server
  • Integrate with Pyodide for Python execution

WebAssembly notebooks

  • Notebooks running entirely in the browser
  • Combine JavaScript, Python, and other languages
  • Enable complex computations client-side

In-tree vs Out-of-tree Builds

  • Different approaches to Pyodide package building
  • In-tree: Built within the Pyodide repository source
  • Out-of-tree: Built separately from source (in your own CI)

Benefits of out-of-tree Builds

  • Enhanced robustness across public API since changes are tested with CI
  • Better runtime support
  • Easier maintenance and updates

My work at Quansight

  • Improving out-of-tree build process
  • Enhancing compatibility with Pyodide
  • Developing tools for package authors

Interactive API Documentation

  • Live code execution in API refs
  • Test methods and classes in real-time
  • Improve developer experience

Pyodide build system

  • pyodide-build tool
  • Automates package compilation for Pyodide
  • Handles dependencies and optimizations

Optimising package sizes

  • Important for browser-based distributions on throttled networks
  • Techniques for reducing binary size: install tags, not building/separating tests
  • This balances functionality and download speed

Build backend improvements

  • Enhancing existing build tools
  • Creating Pyodide-specific optimizations
  • Ensuring cross-platform compatibility

Conclusion

  • Pyodide enables new possibilities for Python
  • Interactive documentation is the future
  • Ongoing work to improve package ecosystem

Thank you!

Questions?