Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Tauri plugin to run python code in the backend instead of rust

License

NotificationsYou must be signed in to change notification settings

marcomq/tauri-plugin-python

Repository files navigation

Thistauri v2 plugin is supposed to make it easy to use Python as backend code.
It usesRustPython or alternativelyPyO3 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 doesn't even supportsome usually built-int python libraries and is slower than PyO3/CPython.PyO3 is supported as optional Cargo feature for desktop applications.PyO3 uses the usual 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 filesrc-tauri/src-python/main.py 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.

PlatformSupported
Linux
Windows
MacOS
Androidx*
iOS✓*

x* There is currently a known issue on tauri+android that prevents reading files.
tauri-apps/tauri#11823
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 (early) 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

UsingPyO3 will support much more python libraries than RustPython as it is using CPython.

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

Unfortunately, using PyO3 will use a shared libpython by default, which makeslocal development easy but makesdeployment of releases more complicated.Therefore, it may be recommended to either usepyoxidizer to embed libpython staticallyor try to ship the dynamic libpython together with your application, for example as partof the .venv. Check out thePyO3 documentation for additional support.

Example of how to embed libpython statically using PyOxidizer:

This has just been tested locally on MacOS. It may be possible that this is more complicated and requires additional steps on your environment.

Install pyoxidizerpip install pyoxidizer in a venv and run it on bash:

pyoxidizer generate-python-embedding-artifacts src-tauri/target/pyembed

Then, add it to your cargo config:

# src-tauri/.cargo/config.tomlPYO3_CONFIG_FILE = {value ="target/pyembed/pyo3-build-config-file.txt",relative =true }

You can check if the release binary has some shared libpython references by runningotool -L tauri_app on MacOs orldd tauri_app on linux.

Example app

There is a sample Desktop application for Windows/Linux/MacOS using this plugin and vanilla
Javascript inexamples/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.

  • runnpm run tauri add python
  • addsrc-tauri/src-python/main.py and modify it according to your needs, for example add
# src-tauri/src-python/main.py_tauri_plugin_functions= ["greet_python"]# make "greet_python" callable from UIdefgreet_python(rust_var):returnstr(rust_var)+" from python"
  • add"bundle": {"resources": [ "src-python/"], totauri.conf.json so that python files are bundled with your application
  • add the plugin in your js, so
    • addimport { callFunction } from 'tauri-plugin-python-api'
    • addoutputEl.textContent = await callFunction("greet_python", [value]) to get the output of the python functiongreet_python with parameter of js variablevalue

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
  • modifypermissions:[] in src-tauri/capabilities/default.json and add "python:default"
  • add filesrc-tauri/src-python/main.py and add python code, for example:
# src-tauri/src-python/main.pydefgreet_python(rust_var):returnstr(rust_var)+" from python"
  • add.plugin(tauri_plugin_python::init_and_register(vec!["greet_python"])) totauri::Builder::default(), usually insrc-tauri/src/lib.rs. This will initialize the plugin and make the python function "greet_python" available from javascript.
  • add javascript for python plugin in the index.html file directly or somewhere in your javascript application. For vanilla javascript / iife, the modules can be found inwindow.__TAURI__.python. For modern #"auto" data-snippet-clipboard-copy-content="import { callFunction } from 'tauri-plugin-python-api'console.log(await callFunction("greet_python", ["input value"]))">
    import{callFunction}from'tauri-plugin-python-api'console.log(awaitcallFunction("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");console.log(awaitcall.greet_python("input value"));

Using a venv

Using a python venv is highly recommended when using pip dependencies.It will be loaded automatically, if the folder is called.venv.It would be recommended to create it in the project root:

python3 -m venv .venvsource .venv/bin/activate# or run the .venv/bin/activate.bat scriptpip install<your_lib>

You need to make sure that the relevant venv foldersinclude andlib arecopied next to thesrc-python tauri resource folder:

tauri.conf.json

"resources": {"src-python/":"src-python/","../.venv/include/":"src-python/.venv/include/","../.venv/lib/":"src-python/.venv/lib/"}

Deployment

The filesrc-python/main.py is always required for the plugin to work correctly.The included resources can be configured in thetauri.conf.json file.You need to make sure that all python files are included in the tauri resource files and thatyour resource file structure is similar to the local python file structure.

There are no other extra steps required forRustPython as it will be linked statically.ForPyO3, 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, for example by using PyOxidizer. In addition, you need
to copy all additional python files so that python files are next to the binary and should also export the .venv folder, if you are using a venv.

The included resources can be configurable in thetauri.conf.json 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 tomake 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 you already know that you just want to develop completely in python, you might want to take a look atpytauri.
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 tauri plugins
  • want to embed python code directly in your javascript

[8]ページ先頭

©2009-2025 Movatter.jp