rubenf
rubenf4mo ago

how do I share logic between scripts

@Ask Inkeep how do I share logic between scripts
3 Replies
Ask Inkeep
Ask Inkeep4mo ago
Thanks for tagging me @rubenf. I'll look through relevant sources and get back to you shortly. ----------------------------- While I generate an answer, here are some sources that might be helpful: * Sharing Common Logic * Dependency Management & Imports * Dependencies in Python * Dependencies in TypeScript ----------------------------- Answer Windmill allows you to share common logic between your scripts using relative imports in both Python and TypeScript. (1) This feature works well with local development and can be easily synced using the CLI. Python Relative Imports For Python scripts, you can import directly from other scripts by following the path layout. (2) Here's an example:
# u/user/common_logic
def foo():
print('Common logic!')

# u/user/custom_script
from u.user.common_logic import foo

def main():
return foo()
# u/user/common_logic
def foo():
print('Common logic!')

# u/user/custom_script
from u.user.common_logic import foo

def main():
return foo()
You can use relative imports for scripts in the same folder or user-space, or access scripts in different folders. TypeScript Relative Imports For TypeScript scripts using Deno or Bun, you can similarly import from other scripts. (3) Here's an example:
import { main as foo, util } from '../my_script_path.ts';
import { main as foo, util } from '../my_script_path.ts';
Ask Inkeep
Ask Inkeep4mo ago
You can use both relative and absolute imports, but relative imports are preferred for compatibility with local editors. Important Considerations 1. In both the web editor and CLI, scripts don't necessarily need a main function. If they don't have one, they're assumed to be shared logic and not runnable scripts. (1) 2. When using relative imports, make sure you have view rights on the scripts you're importing at the time of execution. (2) 3. The folder layout for sharing logic is identical to the one used with the CLI for syncing scripts locally and on Windmill. 4. For TypeScript, path depths can vary, and relative imports will work at any level, ensuring consistency between local and Windmill environments. (3) By using these methods, you can effectively share common logic between your scripts in Windmill, improving code reusability and maintainability. (1) (2) (3)
Ask Inkeep
Ask Inkeep4mo ago
Feel free to tag @Ask Inkeep with additional questions.