Convert Figma logo to code with AI

beeware logobatavia

A JavaScript implementation of the Python virtual machine.

1,387
422
1,387
52

Top Related Projects

12,885

Pyodide is a Python distribution for the browser and Node.js based on WebAssembly

6,451

Brython (Browser Python) is an implementation of Python 3 running in the browser

18,262

PyScript is an open source platform for Python in the browser. Try PyScript: https://pyscript.com Examples: https://tinyurl.com/pyscript-examples Community: https://discord.gg/HxvBtukrg2

3,357

Skulpt is a Javascript implementation of the Python programming language

Quick Overview

Batavia is an open-source project that aims to run Python bytecode in the browser. It allows Python code to be executed directly in JavaScript environments, enabling Python developers to create web applications without writing JavaScript.

Pros

  • Enables Python developers to write web applications using familiar Python syntax
  • Allows for the reuse of existing Python libraries in browser-based applications
  • Provides a bridge between Python and JavaScript ecosystems
  • Supports a wide range of Python language features and standard library modules

Cons

  • Performance may be slower compared to native JavaScript applications
  • Not all Python libraries and modules are fully supported
  • Debugging can be challenging due to the translation layer between Python and JavaScript
  • Limited adoption and community support compared to more established web development frameworks

Code Examples

  1. Basic Python code execution in the browser:
from batavia.utils import run_python

python_code = """
print("Hello from Python in the browser!")
"""

run_python(python_code)
  1. Using Python's standard library in the browser:
from batavia.utils import run_python

python_code = """
import random

numbers = [random.randint(1, 100) for _ in range(5)]
print(f"Random numbers: {numbers}")
"""

run_python(python_code)
  1. Interacting with the DOM using Batavia:
from batavia.utils import run_python

python_code = """
from browser import document

def change_text(event):
    document['output'].textContent = "Button clicked!"

document['mybutton'].bind('click', change_text)
"""

run_python(python_code)

Getting Started

To get started with Batavia, follow these steps:

  1. Install Batavia using pip:

    pip install batavia
    
  2. Create a new HTML file with the following content:

    <!DOCTYPE html>
    <html>
    <head>
        <script src="https://cdn.jsdelivr.net/npm/@pybee/batavia@3.4.0/batavia.min.js"></script>
    </head>
    <body>
        <script>
            var vm = new batavia.VirtualMachine();
            vm.run('print("Hello from Python!")');
        </script>
    </body>
    </html>
    
  3. Open the HTML file in a web browser to see the Python code executed.

For more advanced usage and configuration options, refer to the official Batavia documentation.

Competitor Comparisons

12,885

Pyodide is a Python distribution for the browser and Node.js based on WebAssembly

Pros of Pyodide

  • Runs Python in the browser with full access to the JavaScript ecosystem
  • Supports scientific computing libraries like NumPy and Pandas
  • Allows seamless interaction between Python and JavaScript

Cons of Pyodide

  • Larger download size due to including the CPython interpreter
  • Slower initial load times compared to Batavia
  • Limited to browsers that support WebAssembly

Code Comparison

Pyodide:

import micropip
await micropip.install('numpy')
import numpy as np
arr = np.array([1, 2, 3])

Batavia:

from browser import document
def greet(event):
    print("Hello, World!")
document['mybutton'].bind('click', greet)

Pyodide allows direct use of Python libraries, while Batavia focuses on browser integration. Pyodide's approach enables more complex Python applications in the browser, but at the cost of larger file sizes and potentially slower startup times. Batavia, on the other hand, offers a lighter-weight solution for running Python in the browser, but with more limited functionality compared to Pyodide.

6,451

Brython (Browser Python) is an implementation of Python 3 running in the browser

Pros of Brython

  • Runs Python directly in the browser without a separate compilation step
  • Easier integration with existing web technologies and JavaScript libraries
  • More mature project with a larger community and ecosystem

Cons of Brython

  • Limited support for some Python standard library modules
  • Performance can be slower compared to compiled solutions
  • May require additional effort to handle browser compatibility issues

Code Comparison

Brython:

from browser import document, alert

def greet(event):
    alert("Hello, World!")

document["mybutton"].bind("click", greet)

Batavia:

def greet():
    print("Hello, World!")

# Batavia requires compilation to JavaScript before running in the browser
# The equivalent JavaScript would need to be generated separately

Both projects aim to run Python in web browsers, but they take different approaches. Brython interprets Python code directly in the browser, while Batavia compiles Python to JavaScript. This fundamental difference affects their usage, performance, and integration with web technologies.

18,262

PyScript is an open source platform for Python in the browser. Try PyScript: https://pyscript.com Examples: https://tinyurl.com/pyscript-examples Community: https://discord.gg/HxvBtukrg2

Pros of PyScript

  • Simpler setup and integration with HTML, allowing Python code to be embedded directly in web pages
  • Broader browser compatibility and no need for separate compilation steps
  • More active development and community support

Cons of PyScript

  • Larger file sizes and longer initial load times due to including the entire Python runtime
  • Limited access to low-level browser APIs compared to Batavia's direct JavaScript interop
  • Still in early development stages with potential stability issues

Code Comparison

PyScript:

<py-script>
print("Hello, World!")
for i in range(5):
    print(f"Count: {i}")
</py-script>

Batavia:

var batavia = require('@pybee/batavia');
var vm = new batavia.VirtualMachine();
vm.run(`
print("Hello, World!")
for i in range(5):
    print(f"Count: {i}")
`);

Summary

PyScript offers a more straightforward approach for running Python in the browser, with easier integration and broader compatibility. However, it comes with larger file sizes and potential performance issues. Batavia provides more direct control over the JavaScript environment but requires additional setup and compilation steps. The choice between the two depends on specific project requirements and performance considerations.

3,357

Skulpt is a Javascript implementation of the Python programming language

Pros of Skulpt

  • More mature project with longer development history
  • Wider adoption and community support
  • Better documentation and examples available

Cons of Skulpt

  • Limited to browser-based environments
  • Slower execution compared to native Python
  • Not fully compatible with all Python libraries

Code Comparison

Skulpt:

import skulpt
def greet(name):
    print(f"Hello, {name}!")
greet("World")

Batavia:

from batavia import vm
def greet(name):
    print(f"Hello, {name}!")
vm.run(greet, "World")

Key Differences

  • Skulpt focuses on in-browser Python execution, while Batavia aims for broader JavaScript environment compatibility
  • Batavia is part of the larger BeeWare project, offering integration with other tools in the ecosystem
  • Skulpt has a more extensive standard library implementation
  • Batavia provides better support for Python 3 features

Use Cases

  • Skulpt: Educational platforms, interactive coding environments
  • Batavia: Web applications, serverless functions, cross-platform development

Both projects aim to run Python in JavaScript environments, but they differ in approach and target use cases. Skulpt is more established for browser-based applications, while Batavia offers potential for broader JavaScript ecosystem integration.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Batavia is currently on hiatus

The BeeWare project isn't currently focussing on Batavia development. Python on the Web is still a goal that is important to the BeeWare project; however, the approach that is being used by Batavia needs to be reconsidered. In particular, the emergence of WASM as a viable target for web development opens many options for Python on the web that weren't previously possible.

As a result, we're not dedicating any BeeWare resources to Batavia development, and we we're not encouraging other to contribute to VOC at this time.

If you're interested in exploring future directions for Batavia, please get in touch <https://gitter.im/beeware/general>__.

.. image:: http://beeware.org/project/projects/bridges/batavia/batavia.png :width: 72px :target: https://beeware.org/batavia

======= Batavia

|py-version| |pypi-version| |pypi-status| |license| |build-status| |gitter|

.. |py-version| image:: https://img.shields.io/pypi/pyversions/batavia.svg :target: https://pypi.python.org/pypi/batavia .. |pypi-version| image:: https://img.shields.io/pypi/v/batavia.svg :target: https://pypi.python.org/pypi/batavia .. |pypi-status| image:: https://img.shields.io/pypi/status/batavia.svg :target: https://pypi.python.org/pypi/batavia .. |license| image:: https://img.shields.io/pypi/l/batavia.svg :target: https://github.com/beeware/batavia/blob/master/LICENSE .. |build-status| image:: https://beekeeper.beeware.org/projects/beeware/batavia/shield :target: https://beekeeper.beeware.org/projects/beeware/batavia .. |gitter| image:: https://badges.gitter.im/beeware/general.svg :target: https://gitter.im/beeware/general

Batavia is an early alpha project. If it breaks, you get to keep all the shiny pieces.

Batavia is an implementation of the Python virtual machine, written in JavaScript. With Batavia, you can run Python bytecode in your browser.

It honors Python 3.5+ syntax and conventions, and allows you to reference objects and classes defined natively in JavaScript.

Quick Start

Prerequisites


Batavia requires a Python 3.5, 3.6, or 3.7 installation, and a virtualenv to
run it all in.

You also need to have a recent install of `Node.js <https://nodejs.org>`_
(from the 10.x LTR series), and a current version of npm. If
your version of npm is outdated, you can update it using the command::

$ npm install npm@latest -g

Check the `Setting up your environment
<http://beeware.org/contributing/how/first-time/setup/>`_ for configuration help.


Downloading and Installing
  1. Clone the code repositories ::

$ mkdir beeware $ cd beeware $ git clone https://github.com/beeware/batavia

  1. Setup a virtualenv:

(for other environments, see Getting Started <https://batavia.readthedocs.io/en/latest/tutorial/tutorial-0.html>_).

Linux/Unix/Mac

Type the following commands in your terminal ::

$ python3 --version $ python3 -m venv venv $ . venv/bin/activate $ cd batavia $ pip install -e .

Windows

Type the following commands in your terminal ::

> py -3 -m venv venv
> venv\Scripts\activate
> cd batavia
> pip install -e .

Windows (with only conda installed)

Type in the following commands in your terminal ::

pip install virtualenvwrapper-win mkvirtualenv venv workon venv cd batavia pip install -e .

  1. Install Node.js <https://nodejs.org>_.

You must have a recent version of Node; we do our testing using v10.x. Once you've installed Node, you can use it to install the JavaScript dependencies and compile the Batavia library::

$ npm install

  1. Compile the Batavia library and bundle its dependencies

Run the following command in the terminal ::

$ npm run build

For more detailed setup instructions, see the Getting Started tutorial <https://batavia.readthedocs.io/en/latest/tutorial/tutorial-0.html>_

Running Batavia in the browser


After you have setup the local installation of Batavia, you can now run Python in the browser: ::

$ cd testserver
$ pip install -r requirements.txt
$ python manage.py runserver

then open a web browser at `http://localhost:8000 <http://localhost:8000>`_

For more detailed instructions, see the `Python In The Browser
<http://batavia.readthedocs.io/en/latest/tutorial/tutorial-1.html>`_ guide.


Running Batavia in the terminal

If you want to run some Python code from a file in the terminal, you can also run Batavia on Node: ::

$ npm run python /path/to/python/file.py

This will should run the Python file and show output on the terminal.

For more details see Running Python code using Batavia from the command line <http://batavia.readthedocs.io/en/latest/tutorial/tutorial-2.html>_.

Documentation

Documentation for Batavia <http://batavia.readthedocs.io/en/latest/>_ can be found on Read The Docs <https://readthedocs.org>_, including:

  • Getting Started <https://batavia.readthedocs.io/en/latest/tutorial/index.html>__
  • So, why is it called "Batavia"? <https://batavia.readthedocs.io/en/latest/background/faq.html#why-batavia>_
  • More Frequently Asked Questions <https://batavia.readthedocs.io/en/latest/background/faq.html>_.

Contributing

If you'd like to contribute to Batavia development, our guide for first time contributors <http://beeware.org/contributing/how/first-time/>_ will help you get started.

If you experience problems with Batavia, log them on GitHub <https://github.com/beeware/batavia/issues>_.

Community

Batavia is part of the BeeWare suite <http://beeware.org>_. You can talk to the community through:

  • @pybeeware on Twitter <https://twitter.com/pybeeware>_

  • The beeware/general <https://gitter.im/beeware/general>_ channel on Gitter.

We foster a welcoming and respectful community as described in our BeeWare Community Code of Conduct <http://beeware.org/community/behavior/>_.