Skip to content

Commit 4e6fcac

Browse files
committed
update docs wrt to relative imports
1 parent 88e9095 commit 4e6fcac

File tree

2 files changed

+75
-68
lines changed

2 files changed

+75
-68
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.

docs/reference/index.md

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -352,74 +352,6 @@ windmill:
352352
- PIP_TRUSTED_HOST=pypi.org
353353
```
354354

355-
### Python relative imports for sharing common logic
356-
357-
It is possible to import directly from other Python scripts. One can simply
358-
follow the path layout. For instance,
359-
`import foo from f.<foldername>.script_name`. A more complete example below:
360-
361-
```python
362-
# u/user/common_logic
363-
364-
def foo():
365-
print('Common logic!')
366-
```
367-
368-
And in another Script:
369-
370-
```python
371-
# u/user/custom_script
372-
373-
from u.user.common_logic import foo
374-
375-
def main():
376-
return foo()
377-
```
378-
379-
It works with Scripts contained in folders, and for scripts contained in
380-
user-spaces, e.g: `f.<foldername>.script_path` or `u.<username>.script_path`.
381-
382-
You can also do relative imports to the current script. For instance.
383-
384-
```python
385-
# if common_logic is a script in the same folder or user-space
386-
from .common_logic import foo
387-
# otherwise if you need to access the folder 'folder'
388-
from ..folder.common_logic import foo
389-
```
390-
391-
Beware that you can only import scripts that you have view rights on on at time of execution.
392-
Furthermore, if you make any imports in the common logic, you will need to add
393-
the same imports in the Script that is being imported, otherwise the automatic
394-
dependency management will not work.
395-
396-
The folder layout is identical with the one that works with the CLI for syncing
397-
scripts locally and on Windmill. See [Developing scripts locally](../advanced/4_local_development/index.md)
398-
399-
### Deno relative imports for sharing common logic
400-
401-
Similarly to Python, it is possible to import directly from other TypeScript
402-
Scripts. One can simply follow the path layout. For instance,
403-
`import { foo } from "/f/<foldername>/script_name.ts"`. A more verbose example
404-
below:
405-
406-
```typescript
407-
import { main as foo, util } from '/f/common/my_script_path.ts';
408-
409-
export async function main() {
410-
await foo();
411-
util();
412-
}
413-
```
414-
415-
You may also use simple relative imports:
416-
417-
```typescript
418-
import { main as foo, util } from '../my_script_path.ts';
419-
```
420-
421-
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.
422-
423355
## Flows
424356

425357
A **Flow** is a core concept. It is a JSON serializable value in the

0 commit comments

Comments
 (0)