|
| 1 | +if ('__TAURI__' in window) { |
| 2 | +var __TAURI_PYTHON_PLUGIN_API__ = (function (exports) { |
| 3 | + 'use strict'; |
| 4 | + |
| 5 | + /****************************************************************************** |
| 6 | + Copyright (c) Microsoft Corporation. |
| 7 | +
|
| 8 | + Permission to use, copy, modify, and/or distribute this software for any |
| 9 | + purpose with or without fee is hereby granted. |
| 10 | +
|
| 11 | + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 12 | + REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 13 | + AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 14 | + INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 15 | + LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 16 | + OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 17 | + PERFORMANCE OF THIS SOFTWARE. |
| 18 | + ***************************************************************************** */ |
| 19 | + /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ |
| 20 | + |
| 21 | + |
| 22 | + typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { |
| 23 | + var e = new Error(message); |
| 24 | + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; |
| 25 | + }; |
| 26 | + |
| 27 | + /** |
| 28 | + * Sends a message to the backend. |
| 29 | + * @example |
| 30 | + * ```typescript |
| 31 | + * import { invoke } from '@tauri-apps/api/core'; |
| 32 | + * await invoke('login', { user: 'tauri', password: 'poiwe3h4r5ip3yrhtew9ty' }); |
| 33 | + * ``` |
| 34 | + * |
| 35 | + * @param cmd The command name. |
| 36 | + * @param args The optional arguments to pass to the command. |
| 37 | + * @param options The request options. |
| 38 | + * @return A promise resolving or rejecting to the backend response. |
| 39 | + * |
| 40 | + * @since 1.0.0 |
| 41 | + */ |
| 42 | + async function invoke(cmd, args = {}, options) { |
| 43 | + return window.__TAURI_INTERNALS__.invoke(cmd, args, options); |
| 44 | + } |
| 45 | + |
| 46 | + /** Tauri Python Plugin |
| 47 | + * © Copyright 2024, by Marco Mengelkoch |
| 48 | + * Licensed under MIT License, see License file for more details |
| 49 | + * git clonehttps://github.com/marcomq/tauri-plugin-python |
| 50 | + **/ |
| 51 | + let call = {}; // array of functions |
| 52 | + async function runPython(code) { |
| 53 | + return await invoke('plugin:python|run_python', { |
| 54 | + payload: { |
| 55 | + value: code, |
| 56 | + }, |
| 57 | + }).then((r) => { |
| 58 | + return r.value; |
| 59 | + }); |
| 60 | + } |
| 61 | + /** |
| 62 | + * Regeisters function on server and makes it available via `call.{jsFunctionName}` |
| 63 | + * @param {string} pythonFunctionCall - The python function call, can contain one dot |
| 64 | + * @param {number} [numberOfArgs] - Number of arguments, used for validation in pythons, use -1 to ignore this value |
| 65 | + * @param {string} [jsFunctionName] - Name that is used in javscript: "call.jsFunctionName". Must not contain dots. |
| 66 | + */ |
| 67 | + async function registerFunction(pythonFunctionCall, numberOfArgs, jsFunctionName) { |
| 68 | + if (numberOfArgs !== undefined && numberOfArgs < 0) { |
| 69 | + numberOfArgs = undefined; |
| 70 | + } |
| 71 | + return await invoke('plugin:python|register_function', { |
| 72 | + payload: { |
| 73 | + pythonFunctionCall, |
| 74 | + numberOfArgs |
| 75 | + }, |
| 76 | + }).then((r) => { |
| 77 | + registerJs(pythonFunctionCall, jsFunctionName); |
| 78 | + return r.value; |
| 79 | + }); |
| 80 | + } |
| 81 | + /** |
| 82 | + * No server invokation - assumes that function has already been registered server-side |
| 83 | + * Makes function available as `call.{jsFunctionName}` |
| 84 | + * @param {string} pythonFunctionCall - The python function call, can contain one dot |
| 85 | + * @param {string} [jsFunctionName] - Name that is used in javscript: "call.jsFunctionName". Must not contain dots. |
| 86 | + */ |
| 87 | + async function registerJs(pythonFunctionCall, jsFunctionName) { |
| 88 | + if (jsFunctionName === undefined) { |
| 89 | + jsFunctionName = pythonFunctionCall.replace(".", "_"); |
| 90 | + } |
| 91 | + call[jsFunctionName] = function (...args) { return callFunction(pythonFunctionCall, args); }; |
| 92 | + } |
| 93 | + /** |
| 94 | + * calling previously registered function |
| 95 | + */ |
| 96 | + async function callFunction(functionName, args) { |
| 97 | + return invoke('plugin:python|call_function', { |
| 98 | + payload: { |
| 99 | + functionName, |
| 100 | + args, |
| 101 | + }, |
| 102 | + }).then((r) => { |
| 103 | + return r.value; |
| 104 | + }); |
| 105 | + } |
| 106 | + /** |
| 107 | + * read variable name directly from python |
| 108 | + */ |
| 109 | + async function readVariable(value) { |
| 110 | + return invoke('plugin:python|read_variable', { |
| 111 | + payload: { |
| 112 | + value, |
| 113 | + }, |
| 114 | + }).then((r) => { |
| 115 | + return r.value; |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + exports.call = call; |
| 120 | + exports.callFunction = callFunction; |
| 121 | + exports.readVariable = readVariable; |
| 122 | + exports.registerFunction = registerFunction; |
| 123 | + exports.registerJs = registerJs; |
| 124 | + exports.runPython = runPython; |
| 125 | + |
| 126 | + return exports; |
| 127 | + |
| 128 | +})({}); |
| 129 | +Object.defineProperty(window.__TAURI__, 'python', { value: __TAURI_PYTHON_PLUGIN_API__ }) } |
0 commit comments