|
| 1 | +# Sharing common logic |
| 2 | + |
| 3 | +It is common to want to share common logic between your scripts. This can be done easily using relative imports in both Python and Deno. |
| 4 | + |
| 5 | +Note that in both the webeditor and with the CLI, your scripts do not necessarily need to have a main function. If they don't, they are asummed to be shared logic and not runnable scripts. |
| 6 | + |
| 7 | +It works extremely well in combination with [Developing scripts locally](../4_local_development/index.md) and you can easily sync your scripts with the [CLI](../3_cli/index.md). |
| 8 | + |
| 9 | +### Python relative imports for sharing common logic |
| 10 | + |
| 11 | +It is possible to import directly from other Python scripts. One can simply |
| 12 | +follow the path layout. For instance, |
| 13 | +`import foo from f.<foldername>.script_name`. A more complete example below: |
| 14 | + |
| 15 | +```python |
| 16 | +# u/user/common_logic |
| 17 | + |
| 18 | +def foo(): |
| 19 | + print('Common logic!') |
| 20 | +``` |
| 21 | + |
| 22 | +And in another Script: |
| 23 | + |
| 24 | +```python |
| 25 | +# u/user/custom_script |
| 26 | + |
| 27 | +from u.user.common_logic import foo |
| 28 | + |
| 29 | +def main(): |
| 30 | + return foo() |
| 31 | +``` |
| 32 | + |
| 33 | +It works with Scripts contained in folders, and for scripts contained in |
| 34 | +user-spaces, e.g: `f.<foldername>.script_path` or `u.<username>.script_path`. |
| 35 | + |
| 36 | +You can also do relative imports to the current script. For instance. |
| 37 | + |
| 38 | +```python |
| 39 | +# if common_logic is a script in the same folder or user-space |
| 40 | +from .common_logic import foo |
| 41 | +# otherwise if you need to access the folder 'folder' |
| 42 | +from ..folder.common_logic import foo |
| 43 | +``` |
| 44 | + |
| 45 | +Beware that you can only import scripts that you have view rights on on at time of execution. |
| 46 | +Furthermore, if you make any imports in the common logic, you will need to add |
| 47 | +the same imports in the Script that is being imported, otherwise the automatic |
| 48 | +dependency management will not work. |
| 49 | + |
| 50 | +The folder layout is identical with the one that works with the CLI for syncing |
| 51 | +scripts locally and on Windmill. See [Developing scripts locally](../4_local_development/index.md) |
| 52 | + |
| 53 | +### Deno relative imports for sharing common logic |
| 54 | + |
| 55 | +Similarly to Python, it is possible to import directly from other TypeScript |
| 56 | +Scripts. One can simply follow the path layout. For instance, |
| 57 | +`import { foo } from "/f/<foldername>/script_name.ts"`. A more verbose example |
| 58 | +below: |
| 59 | + |
| 60 | +```typescript |
| 61 | +import { main as foo, util } from '/f/common/my_script_path.ts'; |
| 62 | + |
| 63 | +export async function main() { |
| 64 | + await foo(); |
| 65 | + util(); |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +You may also use simple relative imports: |
| 70 | + |
| 71 | +```typescript |
| 72 | +import { main as foo, util } from '../my_script_path.ts'; |
| 73 | +``` |
| 74 | + |
| 75 | +Note that path in windmill can have as many depth as needed, so you can have paths like this `f/folder/subfolder/my_script_path.ts` and relative imports will work at any level. Hence, it will work exactly the same as on local. |
0 commit comments