Pyscript File Handling
Contents
[Work in progress]
Useful PyScript file handling examples for all combinations: local/remote files and import/export.
Intro
This blog post is the result of this discussion on GitHub and is supposed to supply boilerplate code for simple file handling in PyScript.
There are two main operators import
and export
for two different data types non-binary
like string data of a csv and binary
like xlsx, pdf etc. as well a two different modes local
or remote
files where local means your local file system and remote intends remotely stored files under a different domain (URI).
The following 6 of the following 8 combinations are adressed here:
|
|
Remote exports however do not make much sense. If you have access to whatever file system, it will always be the export local workflow. Else you would need to have some kind of API/Server listening which is not covered here.
Usage
Jump to the respective section on the right and copy the code of interest. Head to the bottom of this page, paste it in the blue execution cell and execute it with a click on the play button.
1. Import
1.1 Local
1.1.1 Non-binary
This example is based on John Hanley’s version.
Uses the browser’s file picker, loads any file and prints it.
Steps:
- Optional: e.g. download this small test csv or set up any other text-based file on your device.
- Copy the below code, paste it in the blue execution cell and execute it with a click on the play button.
- Click the
Read Local File
button and choose the file - If everything worked, you will see the file’s content printed below.
|
|
This code simply prints the file content as text. From here on you could just as well read in pandas or display it formatted as proper HTML.
1.1.2 Binary
Same to above.
Steps:
- Optional: e.g. download this small test xlsx
- Copy the below code, paste it in the blue execution cell and execute it with a click on the play button.
- Click the
Read Local File
button and choose the file - If everything worked, you will see the file’s content printed below.