Skip to content

Commit c480d14

Browse files
committed
adding dist-js
1 parent 53f35c5 commit c480d14

5 files changed

Lines changed: 319 additions & 1 deletion

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ yarn.lock
1313
Cargo.lock
1414
node_modules/
1515

16-
dist-js
1716
dist

dist-js/index.cjs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'use strict';
2+
3+
var core = require('@tauri-apps/api/core');
4+
5+
/** Tauri Python Plugin
6+
* © Copyright 2024, by Marco Mengelkoch
7+
* Licensed under MIT License, see License file for more details
8+
* git clonehttps://github.com/marcomq/tauri-plugin-python
9+
**/
10+
let call = {}; // array of functions
11+
async function runPython(code) {
12+
return await core.invoke('plugin:python|run_python', {
13+
payload: {
14+
value: code,
15+
},
16+
}).then((r) => {
17+
return r.value;
18+
});
19+
}
20+
/**
21+
* Regeisters function on server and makes it available via `call.{jsFunctionName}`
22+
* @param {string} pythonFunctionCall - The python function call, can contain one dot
23+
* @param {number} [numberOfArgs] - Number of arguments, used for validation in pythons, use -1 to ignore this value
24+
* @param {string} [jsFunctionName] - Name that is used in javscript: "call.jsFunctionName". Must not contain dots.
25+
*/
26+
async function registerFunction(pythonFunctionCall, numberOfArgs, jsFunctionName) {
27+
if (numberOfArgs !== undefined && numberOfArgs < 0) {
28+
numberOfArgs = undefined;
29+
}
30+
return await core.invoke('plugin:python|register_function', {
31+
payload: {
32+
pythonFunctionCall,
33+
numberOfArgs
34+
},
35+
}).then((r) => {
36+
registerJs(pythonFunctionCall, jsFunctionName);
37+
return r.value;
38+
});
39+
}
40+
/**
41+
* No server invokation - assumes that function has already been registered server-side
42+
* Makes function available as `call.{jsFunctionName}`
43+
* @param {string} pythonFunctionCall - The python function call, can contain one dot
44+
* @param {string} [jsFunctionName] - Name that is used in javscript: "call.jsFunctionName". Must not contain dots.
45+
*/
46+
async function registerJs(pythonFunctionCall, jsFunctionName) {
47+
if (jsFunctionName === undefined) {
48+
jsFunctionName = pythonFunctionCall.replace(".", "_");
49+
}
50+
call[jsFunctionName] = function (...args) { return callFunction(pythonFunctionCall, args); };
51+
}
52+
/**
53+
* calling previously registered function
54+
*/
55+
async function callFunction(functionName, args) {
56+
return core.invoke('plugin:python|call_function', {
57+
payload: {
58+
functionName,
59+
args,
60+
},
61+
}).then((r) => {
62+
return r.value;
63+
});
64+
}
65+
/**
66+
* read variable name directly from python
67+
*/
68+
async function readVariable(value) {
69+
return core.invoke('plugin:python|read_variable', {
70+
payload: {
71+
value,
72+
},
73+
}).then((r) => {
74+
return r.value;
75+
});
76+
}
77+
78+
exports.call = call;
79+
exports.callFunction = callFunction;
80+
exports.readVariable = readVariable;
81+
exports.registerFunction = registerFunction;
82+
exports.registerJs = registerJs;
83+
exports.runPython = runPython;

dist-js/index.d.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/** Tauri Python Plugin
2+
* © Copyright 2024, by Marco Mengelkoch
3+
* Licensed under MIT License, see License file for more details
4+
* git clonehttps://github.com/marcomq/tauri-plugin-python
5+
**/
6+
export declare let call: {
7+
[index: string]: Function;
8+
};
9+
export declare function runPython(code: string): Promise<string>;
10+
/**
11+
* Regeisters function on server and makes it available via `call.{jsFunctionName}`
12+
* @param {string} pythonFunctionCall - The python function call, can contain one dot
13+
* @param {number} [numberOfArgs] - Number of arguments, used for validation in pythons, use -1 to ignore this value
14+
* @param {string} [jsFunctionName] - Name that is used in javscript: "call.jsFunctionName". Must not contain dots.
15+
*/
16+
export declare function registerFunction(pythonFunctionCall: string, numberOfArgs?: number, jsFunctionName?: string): Promise<string>;
17+
/**
18+
* No server invokation - assumes that function has already been registered server-side
19+
* Makes function available as `call.{jsFunctionName}`
20+
* @param {string} pythonFunctionCall - The python function call, can contain one dot
21+
* @param {string} [jsFunctionName] - Name that is used in javscript: "call.jsFunctionName". Must not contain dots.
22+
*/
23+
export declare function registerJs(pythonFunctionCall: string, jsFunctionName?: string): Promise<void>;
24+
/**
25+
* calling previously registered function
26+
*/
27+
export declare function callFunction(functionName: string, args: any[]): Promise<string>;
28+
/**
29+
* read variable name directly from python
30+
*/
31+
export declare function readVariable(value: string): Promise<string>;

dist-js/index.iife.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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__ }) }

dist-js/index.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { invoke } from '@tauri-apps/api/core';
2+
3+
/** Tauri Python Plugin
4+
* © Copyright 2024, by Marco Mengelkoch
5+
* Licensed under MIT License, see License file for more details
6+
* git clonehttps://github.com/marcomq/tauri-plugin-python
7+
**/
8+
let call = {}; // array of functions
9+
async function runPython(code) {
10+
return await invoke('plugin:python|run_python', {
11+
payload: {
12+
value: code,
13+
},
14+
}).then((r) => {
15+
return r.value;
16+
});
17+
}
18+
/**
19+
* Regeisters function on server and makes it available via `call.{jsFunctionName}`
20+
* @param {string} pythonFunctionCall - The python function call, can contain one dot
21+
* @param {number} [numberOfArgs] - Number of arguments, used for validation in pythons, use -1 to ignore this value
22+
* @param {string} [jsFunctionName] - Name that is used in javscript: "call.jsFunctionName". Must not contain dots.
23+
*/
24+
async function registerFunction(pythonFunctionCall, numberOfArgs, jsFunctionName) {
25+
if (numberOfArgs !== undefined && numberOfArgs < 0) {
26+
numberOfArgs = undefined;
27+
}
28+
return await invoke('plugin:python|register_function', {
29+
payload: {
30+
pythonFunctionCall,
31+
numberOfArgs
32+
},
33+
}).then((r) => {
34+
registerJs(pythonFunctionCall, jsFunctionName);
35+
return r.value;
36+
});
37+
}
38+
/**
39+
* No server invokation - assumes that function has already been registered server-side
40+
* Makes function available as `call.{jsFunctionName}`
41+
* @param {string} pythonFunctionCall - The python function call, can contain one dot
42+
* @param {string} [jsFunctionName] - Name that is used in javscript: "call.jsFunctionName". Must not contain dots.
43+
*/
44+
async function registerJs(pythonFunctionCall, jsFunctionName) {
45+
if (jsFunctionName === undefined) {
46+
jsFunctionName = pythonFunctionCall.replace(".", "_");
47+
}
48+
call[jsFunctionName] = function (...args) { return callFunction(pythonFunctionCall, args); };
49+
}
50+
/**
51+
* calling previously registered function
52+
*/
53+
async function callFunction(functionName, args) {
54+
return invoke('plugin:python|call_function', {
55+
payload: {
56+
functionName,
57+
args,
58+
},
59+
}).then((r) => {
60+
return r.value;
61+
});
62+
}
63+
/**
64+
* read variable name directly from python
65+
*/
66+
async function readVariable(value) {
67+
return invoke('plugin:python|read_variable', {
68+
payload: {
69+
value,
70+
},
71+
}).then((r) => {
72+
return r.value;
73+
});
74+
}
75+
76+
export { call, callFunction, readVariable, registerFunction, registerJs, runPython };

0 commit comments

Comments
 (0)