mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-09-07 08:02:40 +00:00
fix(frontend): Export python code as string (#1339)
This commit is contained in:
+2
-2
@@ -1,4 +1,4 @@
|
||||
import os
|
||||
export default `import os
|
||||
|
||||
# flow is considered recovered and a success unless an exception is raised
|
||||
|
||||
@@ -7,4 +7,4 @@ def main(message: str, name: str):
|
||||
flow_id = os.environ.get("WM_FLOW_JOB_ID")
|
||||
print("message", message)
|
||||
print("name", name)
|
||||
return message, flow_id
|
||||
return message, flow_id`
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
import os
|
||||
export default `import os
|
||||
import wmill
|
||||
|
||||
# You can import any package from PyPI, even if the assistant complains
|
||||
@@ -22,7 +22,7 @@ def main(
|
||||
secret = wmill.get_variable("f/examples/secret")
|
||||
except:
|
||||
secret = "No secret yet at f/examples/secret !"
|
||||
print(f"The variable at `f/examples/secret`: {secret}")
|
||||
print(f"The variable at \`f/examples/secret\`: {secret}")
|
||||
|
||||
# Get last state of this script execution by the same trigger/user
|
||||
last_state = wmill.get_state()
|
||||
@@ -34,4 +34,4 @@ def main(
|
||||
user = os.environ.get("WM_USERNAME")
|
||||
|
||||
# return value is converted to JSON
|
||||
return {"splitted": name.split(), "user": user, "state": new_state}
|
||||
return {"splitted": name.split(), "user": user, "state": new_state}`
|
||||
@@ -1,5 +0,0 @@
|
||||
# import wmill
|
||||
|
||||
|
||||
def main(x: str):
|
||||
return x
|
||||
@@ -0,0 +1,5 @@
|
||||
export default `# import wmill
|
||||
|
||||
|
||||
def main(x: str):
|
||||
return x`
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
import wmill
|
||||
export default `import wmill
|
||||
|
||||
|
||||
def main():
|
||||
@@ -11,4 +11,4 @@ def main():
|
||||
# wmill.setState(newState)
|
||||
# 4. Return the new rows
|
||||
# return range from (state to newState)
|
||||
return [1, 2, 3]
|
||||
return [1, 2, 3]`
|
||||
@@ -1,11 +1,16 @@
|
||||
import type { Script } from "./gen"
|
||||
import type { Script } from './gen'
|
||||
|
||||
import PYTHON_INIT_CODE from '$lib/init_scripts/python_init_code.py?raw'
|
||||
import PYTHON_INIT_CODE_CLEAR from '$lib/init_scripts/python_init_code_clear.py?raw'
|
||||
import PYTHON_INIT_CODE_TRIGGER from '$lib/init_scripts/python_init_code_trigger.py?raw'
|
||||
import PYTHON_FAILURE_MODULE_CODE from '$lib/init_scripts/python_failure_module.py?raw'
|
||||
import PYTHON_INIT_CODE from '$lib/init_scripts/python_init_code'
|
||||
import PYTHON_INIT_CODE_CLEAR from '$lib/init_scripts/python_init_code_clear'
|
||||
import PYTHON_INIT_CODE_TRIGGER from '$lib/init_scripts/python_init_code_trigger'
|
||||
import PYTHON_FAILURE_MODULE_CODE from '$lib/init_scripts/python_failure_module'
|
||||
|
||||
export { PYTHON_INIT_CODE, PYTHON_INIT_CODE_CLEAR, PYTHON_INIT_CODE_TRIGGER, PYTHON_FAILURE_MODULE_CODE }
|
||||
export {
|
||||
PYTHON_INIT_CODE,
|
||||
PYTHON_INIT_CODE_CLEAR,
|
||||
PYTHON_INIT_CODE_TRIGGER,
|
||||
PYTHON_FAILURE_MODULE_CODE
|
||||
}
|
||||
|
||||
export const DENO_INIT_CODE = `// Ctrl+. to cache dependencies on imports hover, Ctrl+S to format.
|
||||
|
||||
@@ -77,7 +82,6 @@ export async function main(message: string, name: string) {
|
||||
}
|
||||
`
|
||||
|
||||
|
||||
export const POSTGRES_INIT_CODE = `import {
|
||||
pgSql,
|
||||
type Resource,
|
||||
@@ -110,7 +114,7 @@ export async function main(
|
||||
db
|
||||
)\`INSERT INTO demo VALUES (\${key}, \${value})\`;
|
||||
return query.rows;
|
||||
}`;
|
||||
}`
|
||||
|
||||
export const BASH_INIT_CODE = `# arguments of the form X="$I" are parsed as parameters X of type string
|
||||
msg="$1"
|
||||
@@ -120,7 +124,6 @@ dflt="\${2:-default value}"
|
||||
echo "Hello $msg"
|
||||
`
|
||||
|
||||
|
||||
export const DENO_INIT_CODE_TRIGGER = `import * as wmill from "https://deno.land/x/windmill@v${__pkg__.version}/mod.ts"
|
||||
|
||||
export async function main() {
|
||||
@@ -172,61 +175,71 @@ export async function main(approver?: string) {
|
||||
return wmill.getResumeEndpoints(approver)
|
||||
}`
|
||||
|
||||
const ALL_INITIAL_CODE = [PYTHON_INIT_CODE, PYTHON_INIT_CODE_TRIGGER, DENO_INIT_CODE, POSTGRES_INIT_CODE, DENO_INIT_CODE_TRIGGER, DENO_INIT_CODE_CLEAR, PYTHON_INIT_CODE_CLEAR, DENO_INIT_CODE_APPROVAL, DENO_FAILURE_MODULE_CODE]
|
||||
const ALL_INITIAL_CODE = [
|
||||
PYTHON_INIT_CODE,
|
||||
PYTHON_INIT_CODE_TRIGGER,
|
||||
DENO_INIT_CODE,
|
||||
POSTGRES_INIT_CODE,
|
||||
DENO_INIT_CODE_TRIGGER,
|
||||
DENO_INIT_CODE_CLEAR,
|
||||
PYTHON_INIT_CODE_CLEAR,
|
||||
DENO_INIT_CODE_APPROVAL,
|
||||
DENO_FAILURE_MODULE_CODE
|
||||
]
|
||||
|
||||
export function isInitialCode(content: string): boolean {
|
||||
for (const code of ALL_INITIAL_CODE) {
|
||||
if (content === code) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
for (const code of ALL_INITIAL_CODE) {
|
||||
if (content === code) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export function initialCode(language: 'deno' | 'python3' | 'go' | 'bash', kind: Script.kind, subkind: 'pgsql' | 'mysql' | 'flow' | 'script' | undefined): string {
|
||||
|
||||
if (language === 'deno') {
|
||||
if (kind === 'trigger') {
|
||||
return DENO_INIT_CODE_TRIGGER
|
||||
} else if (kind === 'script') {
|
||||
if (subkind === 'flow') {
|
||||
return DENO_INIT_CODE_CLEAR
|
||||
}
|
||||
else if (subkind === 'pgsql') {
|
||||
return POSTGRES_INIT_CODE
|
||||
}
|
||||
else if (subkind === 'mysql') {
|
||||
return MYSQL_INIT_CODE
|
||||
} else {
|
||||
return DENO_INIT_CODE
|
||||
}
|
||||
} else if (kind === 'failure') {
|
||||
return DENO_FAILURE_MODULE_CODE
|
||||
} else if (kind === 'approval') {
|
||||
return DENO_INIT_CODE_APPROVAL
|
||||
}
|
||||
else {
|
||||
return DENO_INIT_CODE
|
||||
}
|
||||
} else if (language === 'python3') {
|
||||
if (subkind === 'flow') {
|
||||
return PYTHON_INIT_CODE_CLEAR
|
||||
} else if (kind === 'failure') {
|
||||
return PYTHON_FAILURE_MODULE_CODE
|
||||
} else if (kind === 'trigger') {
|
||||
return PYTHON_INIT_CODE_TRIGGER
|
||||
} else {
|
||||
return PYTHON_INIT_CODE
|
||||
}
|
||||
} else if (language == 'bash') {
|
||||
return BASH_INIT_CODE
|
||||
} else {
|
||||
if (kind === 'failure') {
|
||||
return GO_FAILURE_MODULE_CODE
|
||||
} else if (kind === 'trigger') {
|
||||
return GO_INIT_CODE_TRIGGER
|
||||
} else {
|
||||
return GO_INIT_CODE
|
||||
}
|
||||
}
|
||||
export function initialCode(
|
||||
language: 'deno' | 'python3' | 'go' | 'bash',
|
||||
kind: Script.kind,
|
||||
subkind: 'pgsql' | 'mysql' | 'flow' | 'script' | undefined
|
||||
): string {
|
||||
if (language === 'deno') {
|
||||
if (kind === 'trigger') {
|
||||
return DENO_INIT_CODE_TRIGGER
|
||||
} else if (kind === 'script') {
|
||||
if (subkind === 'flow') {
|
||||
return DENO_INIT_CODE_CLEAR
|
||||
} else if (subkind === 'pgsql') {
|
||||
return POSTGRES_INIT_CODE
|
||||
} else if (subkind === 'mysql') {
|
||||
return MYSQL_INIT_CODE
|
||||
} else {
|
||||
return DENO_INIT_CODE
|
||||
}
|
||||
} else if (kind === 'failure') {
|
||||
return DENO_FAILURE_MODULE_CODE
|
||||
} else if (kind === 'approval') {
|
||||
return DENO_INIT_CODE_APPROVAL
|
||||
} else {
|
||||
return DENO_INIT_CODE
|
||||
}
|
||||
} else if (language === 'python3') {
|
||||
if (subkind === 'flow') {
|
||||
return PYTHON_INIT_CODE_CLEAR
|
||||
} else if (kind === 'failure') {
|
||||
return PYTHON_FAILURE_MODULE_CODE
|
||||
} else if (kind === 'trigger') {
|
||||
return PYTHON_INIT_CODE_TRIGGER
|
||||
} else {
|
||||
return PYTHON_INIT_CODE
|
||||
}
|
||||
} else if (language == 'bash') {
|
||||
return BASH_INIT_CODE
|
||||
} else {
|
||||
if (kind === 'failure') {
|
||||
return GO_FAILURE_MODULE_CODE
|
||||
} else if (kind === 'trigger') {
|
||||
return GO_INIT_CODE_TRIGGER
|
||||
} else {
|
||||
return GO_INIT_CODE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,9 @@ const config = {
|
||||
postcss: true
|
||||
})
|
||||
],
|
||||
package: {
|
||||
dir: 'package'
|
||||
},
|
||||
|
||||
kit: {
|
||||
adapter:
|
||||
|
||||
Reference in New Issue
Block a user