DEV Community

Cover image for Building a WebAssembly-Powered Python Application with Umi.
Deenanath Dayal
Deenanath Dayal

Posted on

Building a WebAssembly-Powered Python Application with Umi.

Introduction

In this tutorial, we'll explore the integration of Umi, a framework primarily designed for JavaScript, with a Python-based web application. Umi allows us to run WebAssembly (Wasm) code in the browser, opening up exciting possibilities for combining Python and Rust for web development.

We will create a simple application that adds two numbers using Rust and WebAssembly within a Python Flask web application. This example demonstrates the power of WebAssembly and how it can be seamlessly integrated into Python projects.

Prerequisites

Before we dive into the code, make sure you have the following installed:

  1. Python
  2. Node.js
  3. Emscripten

Setting Up the Environment

To begin, let's set up our development environment:

  1. Create a new directory for our project:
mkdir umi-python-example
cd umi-python-example
Enter fullscreen mode Exit fullscreen mode
  1. Initialize a Python virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows, use `venv\Scripts\activate`
Enter fullscreen mode Exit fullscreen mode
  1. Install the necessary Python packages:
pip install uwsgi flask
Enter fullscreen mode Exit fullscreen mode

Creating a Python Flask Application

Now, let's create a Python Flask application:

  1. Create a Python file called app.py:
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    app.run()
Enter fullscreen mode Exit fullscreen mode
  1. Create a templates folder and an index.html file inside it. This HTML file will serve as the entry point for our Umi application:

templates/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Umi Python Example</title>
</head>
<body>
    <div id="root"></div>
    <script src="/static/umi.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Creating a Umi Application

Now, let's create a Umi application within our project:

  1. Run the following command to create a Umi app:
npx create-umi@2
Enter fullscreen mode Exit fullscreen mode

Follow the prompts to configure your Umi project. Make sure to select the appropriate options for your use case.

  1. In the umi/src/pages/index.tsx file, we'll create a simple Rust function that adds two numbers using WebAssembly:
import React from 'react';
import styles from './index.css';

const wasm = import('../../pkg');

const App: React.FC = () => {
  const [result, setResult] = React.useState<number | null>(null);

  React.useEffect(() => {
    wasm
      .then((module) => module.add(4, 5))
      .then((sum) => setResult(sum));
  }, []);

  return (
    <div className={styles.container}>
      <h1>Umi Python Example</h1>
      <p>Result of adding 4 and 5 using Rust and WebAssembly:</p>
      <p>{result}</p>
    </div>
  );
};

export default App;
Enter fullscreen mode Exit fullscreen mode

Building and Running the Application

Now that our application is set up, let's build and run it:

  1. Build the Umi application:
cd umi
npm install
npm run build
Enter fullscreen mode Exit fullscreen mode
  1. Copy the Umi build output to your Flask application's static folder:
cp -r dist ../static
Enter fullscreen mode Exit fullscreen mode
  1. Run the Flask application:
python app.py
Enter fullscreen mode Exit fullscreen mode

Visit http://localhost:5000 in your web browser to see your Umi-powered Python application in action.

Conclusion

In this tutorial, we've explored the integration of Umi, a JavaScript-based framework, into a Python Flask web application. By leveraging WebAssembly and Rust, we were able to add two numbers using a Umi-powered module, demonstrating the potential of combining different technologies for web development.

This example serves as a starting point for exploring more complex integrations and use cases. Umi's flexibility allows developers to harness the power of WebAssembly alongside their existing Python applications, creating a richer and more dynamic web experience.

Top comments (1)

Collapse
 
unknown123 profile image
Unknown

I Like this tutorial .But i have a question , hope you will clear that .

When integrating Python code into a WebAssembly application using Umi, what are the key challenges in managing interoperability between Python and JavaScript environments, and how does Umi facilitate this process?