### Install Plugin NPM Dependency Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md Install the JavaScript API for the Python plugin using npm. ```bash $ npm install tauri-plugin-python-api ``` -------------------------------- ### Basic Python Script for Tauri Plugin Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md This is a basic example of a Python script that can be used with the Tauri Python plugin. Ensure this file is added as a Tauri resource to be shipped with your application. ```python ``` -------------------------------- ### Install Plugin Cargo Dependency Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md Add `tauri-plugin-python` as a dependency in your Rust project's `Cargo.toml` file. ```bash $ cargo add tauri-plugin-python ``` -------------------------------- ### Configure PyOxidizer for Static Python Embedding Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md This configuration is for embedding libpython statically using PyOxidizer. It requires installing pyoxidizer and running a command to generate embedding artifacts. This is primarily tested on macOS and may need adjustments for other environments. ```bash pyoxidizer generate-python-embedding-artifacts src-tauri/target/pyembed ``` -------------------------------- ### Create and Activate Python Virtual Environment Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md It is recommended to use a virtual environment (`.venv`) for managing Python dependencies. Activate it using the provided script. ```sh python3 -m venv .venv source .venv/bin/activate # or run the .venv/bin/activate.bat script pip install ``` -------------------------------- ### Initialize and Register Plugin in Rust Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md In your `src-tauri/src/lib.rs`, initialize the Python plugin and register the functions that should be callable from JavaScript. ```rust .plugin(tauri_plugin_python::init_and_register(vec!["greet_python"])). ``` -------------------------------- ### Configure Tauri Bundle Resources Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md Add the `src-python/` directory to your `tauri.conf.json` to ensure Python files are bundled with your application. ```json "bundle": { "resources": [ "src-python/" ], ``` -------------------------------- ### Configure Venv Folders in Tauri Resources Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md Ensure the `include` and `lib` folders from your virtual environment are copied into the Tauri resources alongside your Python code. ```json "resources": { "src-python/": "src-python/", "../.venv/include/": "src-python/.venv/include/", "../.venv/lib/": "src-python/.venv/lib/" } ``` -------------------------------- ### Initialize Python Plugin in Rust Source: https://github.com/marcomq/tauri-plugin-python/blob/main/examples/plain-javascript/README.md Add the Python plugin to your Tauri application's Rust backend. This involves importing the plugin and initializing it with the list of exposed Python functions. ```rust .plugin(tauri_plugin_python::init(["greet_python"])); ``` -------------------------------- ### Configure Tauri Capabilities Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md Add the 'python:default' permission to your `tauri.conf.json`'s capabilities to enable Python plugin functionality. ```json "permissions":[]" in src-tauri/capabilities/default.json and add "python:default" ``` -------------------------------- ### Add PyOxidizer Config to Cargo Config Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md After generating Python embedding artifacts with PyOxidizer, specify the path to the generated configuration file in your src-tauri/.cargo/config.toml. This tells PyO3 where to find the necessary build information. ```toml PYO3_CONFIG_FILE = { value = "target/pyembed/pyo3-build-config-file.txt", relative = true } ``` -------------------------------- ### Enable PyO3 Feature in Cargo.toml Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md To use PyO3 as the Python interpreter, add the `pyo3` feature to the tauri-plugin-python dependency in your Cargo.toml file. This enables wider Python library compatibility but may complicate deployment. ```toml tauri-plugin-python = { version="0.3", features = ["pyo3"] } ``` -------------------------------- ### Enable RunPython Command Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md The 'runPython' command is disabled by default. Enabling it allows injecting Python code directly from JavaScript, which can be useful for rapid prototyping but requires careful security considerations. ```json The "runPython" command is disabled by default via permissions. If enabled, it is possible to inject python code directly via javascript. ``` -------------------------------- ### Register Python Plugin API in JavaScript Source: https://github.com/marcomq/tauri-plugin-python/blob/main/examples/plain-javascript/README.md Include the plugin's JavaScript API in your HTML and register the specific Python function you want to expose to the frontend. ```html ``` ```javascript registerJs("greet_python"); ``` -------------------------------- ### Import and Call Python Function in JavaScript Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md Import the `callFunction` utility and use it to invoke a registered Python function. Pass any serializable JavaScript value as an argument. ```javascript import { callFunction } from 'tauri-plugin-python-api' outputEl.textContent = await callFunction("greet_python", [value]) ``` -------------------------------- ### Call Python Function with Alternative JavaScript API Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md Use the alternative `call` and `registerJs` API for a more readable way to interact with Python functions. First, register the function name, then call it using dot notation. ```javascript import { call, registerJs } from 'tauri-plugin-python-api' registerJs("greet_python"); console.log(await call.greet_python("input value")); ``` -------------------------------- ### Enable JavaScript Function Registration Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md The 'register' function is disabled by default. Enabling it allows adding control from JavaScript regarding which functions can be called, avoiding modifications to Rust code when changing or adding Python code. ```json 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. ``` -------------------------------- ### Define Python Function for Tauri Source: https://github.com/marcomq/tauri-plugin-python/blob/main/examples/plain-javascript/README.md Write the Python function that will be exposed to your Tauri application. This function can then be called from the frontend via the plugin. ```python def greet_python(..) ``` -------------------------------- ### Python Function Definition Source: https://github.com/marcomq/tauri-plugin-python/blob/main/README.md Define a Python function that can be called from your Tauri application. Ensure the function name is registered in `tauri.conf.json`. ```python # src-tauri/src-python/main.py _tauri_plugin_functions = ["greet_python"] # make "greet_python" callable from UI def greet_python(rust_var): return str(rust_var) + " from python" ``` ```python # src-tauri/src-python/main.py def greet_python(rust_var): return str(rust_var) + " from python" ``` -------------------------------- ### Call Python Function from JavaScript Source: https://github.com/marcomq/tauri-plugin-python/blob/main/examples/plain-javascript/README.md Invoke a registered Python function from your JavaScript code. Ensure the function name matches the one registered. ```javascript call.greet_python(...); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.