Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document Pyodide/PyScript usage. #2262

Open
benjeffery opened this issue May 11, 2022 · 5 comments
Open

Document Pyodide/PyScript usage. #2262

benjeffery opened this issue May 11, 2022 · 5 comments
Labels
documentation Documentation
Milestone

Comments

@benjeffery
Copy link
Member

Advertise possibilities, advise of limitations and give an example of loading a ts from a URL and plotting it.

@benjeffery benjeffery added the documentation Documentation label May 11, 2022
@benjeffery benjeffery added this to the Python 0.5.1 (paper) milestone May 11, 2022
@hyanwong
Copy link
Member

How do we load from a URL easily? We marked #1566 as low priority, but maybe this now moves it up the stack?

@benjeffery
Copy link
Member Author

#1566 won't help here, unless we specifically write it to detect it is running in Pyodide. This is because you can't open sockets like normal Python due to the web security model, so modules like requests don't work.
I'll figure out the fetching code - you have to call JavaScript methods to do the fetching.

@hyanwong
Copy link
Member

Ah, yes, of course. I forgot about requests not being allowed. I guess it would also be useful to be able to display SVGs or Matplotlib output, which is going to be more JS faffing.

@benjeffery
Copy link
Member Author

I don't that the faffing there is so bad. Jupyterlite is always an option.

@hyanwong
Copy link
Member

hyanwong commented Mar 22, 2025

There is now a pyodide.http pyfetch function, which works OK. However, you sometimes run foul of CORS restrictions e.g. for trees files stored in GitHub. Here's an example of how to get around this (e.g. you should be able to paste this into a running Pyodide from https://pyodide.org/en/stable/console.html):

import tempfile
from pyodide.http import pyfetch

import micropip
await micropip.install("tszip")
import tszip

async def load_remote_tree_file():
    # URL of the tree file, using corsproxy to avoid CORS issues
    url = "https://corsproxy.io/https://github.com/tskit-dev/tutorials/raw/refs/heads/main/data/viz_ts_selection.trees"
    # Fetch the file using pyfetch
    response = await pyfetch(url)
    # Check if the request was successful
    if response.status == 200:
        # Get the array buffer from the response
        # Save to a temporary file
        with tempfile.NamedTemporaryFile() as f:
            data = await response.bytes()
            f.write(data)
        # Load the tree file with tszip
            ts = tszip.load(f.name)
            print(f"Successfully loaded tree sequence with {ts.num_trees} trees and {ts.num_samples} samples")
            return ts
    else:
        print(f"Failed to fetch file: Status code {response.status}")
        return None
    

ts = await load_remote_tree_file()
print(ts.first().draw_text())

For tutorial material, I presume we would serve the pyodide / jupyterlite instance from the same location as the .trees or .tsz files, so I assume CORS should not be an issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Documentation
Projects
None yet
Development

No branches or pull requests

3 participants