You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a simple module that mimics pyodide.http.pyfetch to make local development for shinylive projects easier. It may work with pyodide in general, but that use case hasn't been tested.
For more information on Shinylive for Python, and general information on how to use additional third party libraries (like this one), see: https://shiny.posit.co/py/docs/shinylive.html
Install
PyPI: pip install pyfetch-mimic
Vendoring:
Copy pyfetch_mimic.py into your own project
How to use
Include the following conditional import statement at the beginning of the module that will use http.pyfetch:
NOTE: This is a work in progress and does not support all pyodide.http.pyfetch functionality yet. I use this in my own production work, and the functionality that currently exists is simply the functionality that I need. If there is a need for additional functionality, please open an issue or pull request.
pyfetch examples
These should all work with python pyodide.http.pyfetch and pyfetch_mimic.http.pyfetch
# Download, save extracted file to local virtual fsimportsysif"pyodide"insys.modules:
frompyodideimporthttpelse:
frompyfetch_mimicimporthttpasyncdefsample():
response=awaithttp.pyfetch("https://some_url/myfiles.zip")
awaitresponse.unpack_archive()
# Download text file to local virtual fs, load into pandasimportpandasaspdimportsysif"pyodide"insys.modules:
frompyodideimporthttpelse:
frompyfetch_mimicimporthttpasyncdefsample():
response=awaithttp.pyfetch(url())
withopen("test.json", mode="wb") asfile:
file.write(awaitresponse.bytes())
df=pd.read_json("test.json")
# Download text file into BytesIO memory buffer, load into pandasfromioimportBytesIOimportsysimportpandasaspdif"pyodide"insys.modules:
frompyodideimporthttpelse:
frompyfetch_mimicimporthttpasyncdefsample():
response=awaithttp.pyfetch("<URL>")
buf=BytesIO(awaitresponse.bytes())
df=pd.read_json(buf)
Testing
Install Test Dependencies
pip install -e '.[tests]'
Run regular tests (verifies test endpoints and tests pyfetch-mimic)