0% found this document useful (0 votes)
32 views3 pages

Tauri Plugin for Python Integration

The tauri-plugin-python v0.3.5 enables the use of Python code in the backend of Tauri applications, utilizing either RustPython or PyO3 as interpreters. While RustPython simplifies deployment by not requiring Python installation, it has compatibility issues and slower performance compared to PyO3, which offers broader library support but requires additional setup. The plugin allows for easy integration of Python functions into Tauri applications, with specific considerations for deployment and security.

Uploaded by

mmanuel.dummy99
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Tauri Plugin for Python Integration

The tauri-plugin-python v0.3.5 enables the use of Python code in the backend of Tauri applications, utilizing either RustPython or PyO3 as interpreters. While RustPython simplifies deployment by not requiring Python installation, it has compatibility issues and slower performance compared to PyO3, which offers broader library support but requires additional setup. The plugin allows for easy integration of Python functions into Tauri applications, with specific considerations for deployment and security.

Uploaded by

mmanuel.dummy99
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

tauri-plugin-python

v0.3.5
A tauri 2 plugin to use python code in the backend.

Tauri Plugin Python


This tauri v2 plugin is supposed to make it easy to use Python as backend code. It
uses RustPython or alternatively PyO3 as interpreter to call python from rust.

RustPython doesn't require python to be installed on the target platform and makes
it therefore easy to deploy your production binary. Unfortunately, it has some
compatibility issues and is slower than PyO3/CPython. PyO3 is also supported as
optional Cargo feature for desktop applications. PyO3 uses CPython as interpreter
and therefore has a wide compatibility for available python libraries. It isn't
used as default as it requires to make libpython available for the target platform,
which can be complicated, especially for mobile targets.

The plugin reads by default the file src-tauri/src-python/[Link] during startup


and runs it immediately. Make sure to add all your python source as tauri resource,
so it is shipped together with your production binaries. Python functions are all
registered during plugin initialization and can get called during application
workflow.

Platform Supported
Linux ✓
Windows ✓
MacOS ✓
Android x*
iOS ✓*
x* There is currently a known issue on tauri+android that prevents reading files.
[Link]
So python code cannot be read on android right now. Android is going to be
supported as soon as reading resource files will be fixed.

✓* Linux, Windows and MacOS support PyO3 and RustPython as interpreter. Android and
IOS currently only support RustPython. Android and iOS might also be able to run
with PyO3 in theory but would require to have CPython to be compiled for the target
platform. I still need to figure out how to cross compile python and PyO3 for iOS
and Android. Ping me if you know how to do that.

You can use this plugin for fast prototypes or for production code. It might be
possible that you want to use some python library or code that is not available for
rust yet. In case that you want to ship production software packages, you need to
make sure to also ship all your python code. If you use PyO3, you also need to ship
libpython too.

Switch from RustPython to PyO3


# src-tauri/[Link]
tauri-plugin-python = { version="0.3", , features = ["pyo3"] }

Example app
There is a sample Desktop application for Windows/Linux/MacOS using this plugin and
vanilla Javascript in examples/plain-javascript.

Add the plugin to an existing tauri application


These steps assume that you already have a basic tauri application available.
Alternatively, you can immediately start with the application in "example"
directory.

run npm run tauri add python


add src-tauri/src-python/[Link] and modify it according to your needs, for example
add
# src-tauri/src-python/[Link]
_tauri_plugin_functions = ["greet_python"] # make "greet_python" callable from UI
def greet_python(rust_var)
return str(rust_var) + " from python"
add "bundle": {"resources": [ "src-python/**/*"], to [Link] so that python
files are bundled with your application
add the plugin in your js, so
add import { callFunction } from 'tauri-plugin-python-api'
add [Link] = await callFunction("greet_python", [value]) to get the
output of the python function greet_python with parameter of js variable value
Check the examples for alternative function calls and code sugar.

Tauri events and calling js from python is currently not supported yet. You would
need to use rust for that.

Alternative manual plugin installation


$ cargo add tauri-plugin-python
$ npm install tauri-plugin-python-api
modify permissions:[] in src-tauri/capabilities/[Link] and add
"python:default"
add file src-tauri/src-python/[Link] and add python code, for example:
# src-tauri/src-python/[Link]
def greet_python(rust_var)
return str(rust_var) + " from python"
add .plugin(tauri_plugin_python::init_and_register(vec!["greet_python")) to
tauri::Builder::default(), usually in src-tauri/src/[Link]. This will initialize
the plugin and make the python function "greet_python" available from javascript.
add javascript for python plugin in the [Link] file directly or in your
somewhere in your javascript application. For vanilla javascript / iife, the
modules can be found in window.__TAURI__.python. For modern javascript:
import { callFunction } from 'tauri-plugin-python-api'
[Link](await callFunction("greet_python", ["input value"]))
-> this will call the python function "greet_python" with parameter "input value".
Of course, you can just pass in any available javascript value. This should work
with "boolean", "integer", "double", "string", "string[]", "double[]" parameter
types.

Alternatively, to have more readable code:

import { call, registerJs } from 'tauri-plugin-python-api'


registerJs("greet_python");
[Link](await call.greet_python("input value"));
Deployment
You either need to have python installed on the target machine or ship the shared
python library with your package. You also may link the python library statically -
PyO3 may do this by default if it finds a static python library. In addition, you
need to copy the python files so that python files are next to the binary.

The file src-python/[Link] is required for the plugin to work correctly. You may
also add additional python files or use a venv environment. The included resources
can be configurable in the [Link] file.

Check the tauri and PyO3 documentation for additional info.

Security considerations
By default, this plugin cannot call arbitrary python code. Python functions can
only be called if registered from rust during plugin initialization. It may still
be possible to read values from python. This can be prevented via additional tauri
permissions.

Keep in mind that this plugin could make it possible to run arbitrary python code
when using all allow permissions. It is therefore highly recommended to make sure
the user interface is not accessible by a network URL in production.

The "runPython" command is disabled by default via permissions. If enabled, it is


possible to inject python code directly via javascript. Also, the function
"register" is disabled by default. If enabled, it can add control from javascript
which functions can be called. This avoids to modify rust code when changing or
adding python code. Both functions can be enabled during development for rapid
prototyping.

Alternatives
If already know that you just want to develop completely in python, you might want
to take a look at pytauri. It is a different approach to have all tauri
functionality completely in python.

This approach here with tauri-plugin-python is more lightweight and it is for you,
if you

still want to write rust code


already have a tauri application and just need a specific python library
just want to simply support rare custom plugins
if you want to embed python code directly in your javascript

Common questions

Powered by AI

To add tauri-plugin-python to an existing Tauri application, you must first ensure you have a basic Tauri application set up. The next steps involve running 'npm run tauri add python', creating 'src-tauri/src-python/main.py', and specifying desired Python functions to be callable from the UI . This involves the declaration of Python functions and their registration within the application initialization . Further, you must include these Python resources within 'tauri.conf.json' for bundling with the application's resources, and import the plugin's JavaScript API to invoke these functions from the frontend . To conclude, modifying configuration files, installing necessary npm packages, and ensuring Python files are alongside the binary during deployment are essential to complete the setup .

To bundle Python code with a Tauri application utilizing tauri-plugin-python, one must specify the Python files as resources in tauri.conf.json. This involves adding a line to the 'bundle' section, such as "resources": ["src-python/**/*"], ensuring the Python scripts are included with the application's distribution package . This step is crucial for making sure that the necessary Python files are accessible alongside the application binaries during runtime, providing the Python functionality as expected .

Security considerations in using tauri-plugin-python involve ensuring that arbitrary Python code cannot be executed. By default, the plugin only allows calling Python functions that are registered from Rust during plugin initialization, preventing unauthorized code execution . Additionally, permissions management is critical to avoid exposing 'runPython' or 'register' commands that can lead to code injection from JavaScript, potentially allowing execution of any Python code . It is recommended to ensure UI access is restricted, particularly avoiding exposure over network URLs in production setups .

To mitigate security vulnerabilities when integrating Python code in Tauri, it's essential to restrict callable Python functions to those explicitly registered during Rust plugin initialization . Avoid enabling the 'runPython' and 'register' commands unless absolutely necessary, as these permit the injection of arbitrary Python code from JavaScript . Always configure application permissions conservatively, disabling any capabilities that could allow unauthorized code execution. Moreover, prevent exposing the application UI over insecure network URLs in production to thwart potential code injection attacks . Regular audits and security tests should be conducted as part of the development lifecycle to identify and rectify any potential vulnerabilities.

The tauri-plugin-python allows integration of Python code in a Tauri application by using either RustPython or PyO3 as the interpreter. RustPython offers ease of deployment as it does not require Python installation on the target platform, allowing the creation of production binaries . However, it has compatibility limitations and slower performance compared to PyO3/CPython. PyO3, while offering wide compatibility with Python libraries, demands the availability of libpython on the target platform, complicating its use on mobile systems . The plugin serves various use cases such as running specific Python libraries not available in Rust, fast prototyping, or production code where Python functions are registered during plugin initialization and can be called from the application workflow .

Deploying a Tauri application with Python functionality involves ensuring that either Python is installed on the target machine or the shared Python library is shipped with the package . You may opt to statically link the Python library using PyO3, which might automatically link it if a static library is detected . Additionally, accompanying Python files must be included next to the binary, ensuring they are correctly bundled during the build process by configuring tauri.conf.json . The deployment should also consider platform-specific requirements, such as handling shared library compatibility on different operating systems and ensuring all necessary permissions and configurations are adhered to prevent security vulnerabilities .

The tauri-plugin-python supports Linux, Windows, and MacOS for both RustPython and PyO3 interpreters . Mobile platforms like Android and iOS currently only support RustPython due to difficulties in compiling CPython for these platforms. Android also faces a specific issue where reading resource files is problematic, preventing Python code from being executed . Resolving this issue will enable broader support on Android .

Tauri-plugin-python is ideal for scenarios where existing Tauri applications primarily in Rust need specific Python libraries or capabilities that Rust does not readily offer. It is lightweight, allowing Python code to be embedded within JavaScript, making it suitable for applications requiring fast prototyping or niche Python library support . Conversely, developing a Tauri application entirely in Python using pytauri is more viable when the entire business logic and UI functionality are to be managed within Python, providing a more cohesive development experience for Python-focused projects . Tauri-plugin-python is optimal when leveraging Rust's capabilities alongside specific Python functionalities, whereas pytauri suits developers prioritizing Python-centric development.

The tauri-plugin-python facilitates the registration of Python functions via Rust during plugin initialization. These functions can then be invoked from the JavaScript frontend by importing 'callFunction' from 'tauri-plugin-python-api'. The Python function is called by invoking 'callFunction' with the Python function's name and parameters as arguments, enabling seamless integration between frontend JavaScript and backend Python logic . Functions like 'greet_python' can be made callable this way, allowing JavaScript to pass parameters directly to Python functions which then return values back to JavaScript .

RustPython is advantageous for deployment because it does not require Python to be installed on the target platform, making distribution straightforward . However, it suffers from compatibility issues with Python libraries and slower performance. In contrast, PyO3 offers better performance and wider compatibility with existing Python libraries due to using CPython as the interpreter . The drawback of PyO3 is its requirement for libpython to be present on target platforms, complicating deployment, particularly on mobile systems where cross-compilation of Python libraries is challenging . Thus, RustPython is suitable for platforms where ease of deployment is critical, whereas PyO3 is more suitable when library compatibility and performance are the priorities.

You might also like