8000 updated docs for fetch (#1019) · DindyalM/pyscriptErrors@b2bbdda · GitHub
[go: up one dir, main page]

Skip to content

Commit b2bbdda

Browse files
updated docs for fetch (pyscript#1019)
1 parent ee2f46c commit b2bbdda

File tree

1 file changed

+180
-3
lines changed

1 file changed

+180
-3
lines changed

docs/reference/elements/py-config.md

Lines changed: 180 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ One can also use both i.e pass the config from `src` attribute as well as specif
9898
```html
9999
<py-config src="./custom.toml">
100100
[[fetch]]
101-
packages = ["numpy"]
101+
files = ["./utils.py"]
102102
</py-config>
103103
```
104104

@@ -108,7 +108,7 @@ This can also be done via JSON using the `type` attribute.
108108
<py-config type="json" src="./custom.json">
109109
{
110110
"fetch": [{
111-
"packages": ["numpy"]
111+
"files": ["./utils.py"]
112112
}]
113113
}
114114
</py-config>
@@ -256,10 +256,187 @@ A fetch configuration consists of the following:
256256
| `from` | string | Base URL for the resource to be fetched. |
257257
| `to_folder` | string | Name of the folder to create in the filesystem. |
258258
| `to_file` | string | Name of the target to create in the filesystem. |
259-
| `files` | List of string | List of files to be downloaded. |
259+
| `files` | List of strings | List of files to be downloaded. |
260260

261261
The parameters `to_file` and `files` shouldn't be supplied together.
262262

263+
#### Mechanism
264+
265+
The `fetch` mechanism works in the following manner:
266+
267+
- If both `files` and `to_file` parameters are supplied: Error!
268+
- `from` defaults to an empty string i.e. `""` to denote relative URLs of the serving directory
269+
- `to_folder` defaults to `.` i.e. the current working directory of the filesystem
270+
- If `files` is specified
271+
- for each `file` present in the `files` array
272+
- the `sourcePath` is calculated as `from + file`
273+
- the `destination` is calculated as `to_folder + file`
274+
- thus, the object is downloaded from `sourcePath` to `destination`
275+
- Else i.e. `files` is NOT specified
276+
- If `to_file` is specified
277+
- the object is downloaded from `from` to `to_folder + to_file`
278+
- Otherwise, calculate the `filename` at the end of `from` i.e. the part after last `/`
279+
- the object is downloaded from `from` to `to_folder + filename at the end of 'from'`
280+
281+
#### Use-Cases
282+
283+
Assumptions:
284+
285+
The directory being served has the following tree structure:
286+
287+
```
288+
content/
289+
├─ index.html <<< File with <py-config>
290+
├─ info.txt
291+
├─ data/
292+
│ ├─ sensordata.csv
293+
├─ packages/
294+
│ ├─ my_package/
295+
│ │ ├─ __init__.py
296+
│ │ ├─ helloworld/
297+
│ │ │ ├─ __init__.py
298+
│ │ │ ├─ greetings.py
299+
```
300+
301+
1. Fetching a single file
302+
303+
```html
304+
<py-config>
305+
[[fetch]]
306+
files = ['info.txt']
307+
</py-config>
308+
```
309+
310+
```html
311+
<py-script>
312+
with open('info.txt', 'r') as fp:
313+
print(fp.read())
314+
</py-script>
315+
```
316+
317+
2. Single File wi 8000 th Renaming
318+
319+
```html
320+
<py-config>
321+
[[fetch]]
322+
from = 'info.txt'
323+
to_file = 'info_loaded_from_web.txt'
324+
</py-config>
325+
```
326+
327+
```html
328+
<py-script>
329+
with open('info_loaded_from_web.txt', 'r') as fp:
330+
print(fp.read())
331+
</py-script>
332+
```
333+
334+
3. Single File to another Directory
335+
336+
```html
337+
<py-config>
338+
[[fetch]]
339+
files = ['info.txt']
340+
to_folder = 'infofiles/loaded_i 8000 nfo'
341+
</py-config>
342+
```
343+
344+
```html
345+
<py-script>
346+
with open('infofiles/loaded_info/info.txt', 'r') as fp:
347+
print(fp.read())
348+
</py-script>
349+
```
350+
351+
4. Single File to another Directory with Renaming
352+
353+
```html
354+
<py-config>
355+
[[fetch]]
356+
from = 'info.txt'
357+
to_folder = 'infofiles/loaded_info'
358+
to_file = 'info_loaded_from_web.txt'
359+
</py-config>
360+
```
361+
362+
```html
363+
<py-script>
364+
with open('infofiles/loaded_info/info_loaded_from_web.txt', 'r') as fp:
365+
print(fp.read())
366+
</py-script>
367+
```
368+
369+
5. Single file from a folder to the current working directory
370+
371+
```html
372+
<py-config>
373+
[[fetch]]
374+
from = 'data/'
375+
files = ['sensordata.csv']
376+
</py-config>
377+
```
378+
379+
```html
380+
<py-script>
381+
with open('./sensordata.csv', 'r') as fp:
382+
print(fp.read())
383+
</py-script>
384+
```
385+
386+
6. Single file from a folder to another folder (i.e. not the current working directory)
387+
388+
```html
389+
<py-config>
390+
[[fetch]]
391+
from = 'data/'
392+
to_folder = './local_data'
393+
files = ['sensordata.csv']
394+
</py-config>
395+
```
396+
397+
```html
398+
<py-script>
399+
with open('./local_data/sensordata.csv', 'r') as fp:
400+
print(fp.read())
401+
</py-script>
402+
```
403+
404+
7. Multiple files preserving directory structure
405+
406+
```html
407+
<py-config>
408+
[[fetch]]
409+
from = 'packages/my_package/'
410+
files = ['__init__.py', 'helloworld/greetings.py', 'helloworld/__init__.py']
411+
to_folder = 'custom_pkg'
412+
</py-config>
413+
```
414+
415+
```html
416+
<py-script>
417+
from custom_pkg.helloworld.greetings import say_hi
418+
print(say_hi())
419+
</py-script>
420+
```
421+
422+
8. From an API endpoint which doesn't end in a filename
423+
424+
```html
425+
<py-config>
426+
[[fetch]]
427+
from = 'https://catfact.ninja/fact'
428+
to_file = './cat_fact.json'
429+
</py-config>
430+
```
431+
432+
```html
433+
<py-script>
434+
import json
435+
with open("cat_fact.json", "r") as fp:
436+
data = json.load(fp)
437+
</py-script>
438+
```
439+
263440
### Runtime
264441

265442
A runtime configuration consists of the following:

0 commit comments

Comments
 (0)
0