3. Getting started for browser

emlearn-micropython runs on most platforms that MicroPython does. This includes running in a web browser, using the Webassembly port of MicroPython. The browser integration is enabled by PyScript.

3.1. Prerequisites

A web browser and a file editor. An CPython 3.10+ installation is also recommended, to act as web server.

3.2. emlearn-micropython build for browser

We publish a pre-built MicroPython for each release. This includes emlearn-micropython as external C modules.

There are two files needed, the micropython.mjs and micropython.wasm. They can be downloaded from:

The latest version can be changed to a tag to have a specific version (0.11.0 or later).

3.3. Setup web page

Create an index.html page with the following contents:

<!DOCTYPE html>
<html lang="en">
<head>
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width,initial-scale=1" />
      <title>emlearn-micropython in browser with PyScript</title>
      <link rel="stylesheet" href="https://pyscript.net/releases/2026.3.1/core.css">
      <script type="module" src="https://pyscript.net/releases/2026.3.1/core.js"></script>
</head>
<body>
  <!-- This should be in a config file, using mpy-config for brevity. -->
  <mpy-config>
    interpreter = "/micropython.mjs"
  </mpy-config>
  <script type="mpy">
    from pyscript import document

    import emlearn_linreg
    import array

    # Predict temperature from (hour_of_day, humidity)
    # y = 0.5*hour - 0.1*humidity + 15  + noise
    X = array.array('f', [
         8, 80,   # 8am, 80% humidity
        12, 60,   # noon
        16, 55,   # 4pm
        20, 70,   # 8pm
         0, 85,   # midnight
    ])
    y = array.array('f', [15.2, 18.4, 20.1, 16.8, 13.5])

    model = emlearn_linreg.new(2, 0.01, 0.5, 0.0001)
    emlearn_linreg.train(model, X, y, max_iterations=100, tolerance=1e-6)

    mse = model.score_mse(X, y)
    print(f"MSE: {mse:.4f}")

    new_sample = array.array('f', [10, 75])  # 10am, 75% humidity
    out = model.predict(new_sample)
    print(f"Predicted temperature: {out:.1f} C")

    document.body.append(f"Input: {list(new_sample)}, prediction: {out:.2f} C")


  </script>
</body>
</html>

Make sure that you have micropython.mjs and micropython.wasm in the same directory.

3.4. Try it out

Start a HTTP server to serve the files

python -m http.server

Open your browser at http://localhost:8000

The MicroPython code using emlearn_linreg from emlearn-micropython should automatically run when you load the page. The webpage should show an output like like:

Input: [10.0, 75.0], prediction: 16.96 C

3.5. Serving from device

On a MicroPython device with networking (like ESP32), it can serve the browser frontend to clients.

We recommend using the excellent MicroDot web framework.

To be offline compatible, download PyScript and the MicroPython build files to your PC, and copy it to the device. Then update the HTML to have the local paths. See PyScript offline documentation for more information.