r-wasm / quarto-live

WebAssembly powered code blocks and exercises for both the R and Python languages in Quarto documents
https://r-wasm.github.io/quarto-live/
MIT License
128 stars 9 forks source link

How to reference / access data resources from a cell #53

Closed Analect closed 2 months ago

Analect commented 2 months ago

Thanks for your efforts with this tooling.

I was playing around with generating some additional meta-data from a rendered quarto site. Per the resources setting in the _quarto.yml file here, I'm able to ensure my data is available in the rendered docs.

Then, for this *.qmd file, I'm passing through those resources. I'm not sure the second resources under pyodide is strictly necessary.

format: live-html
resources:
  - "../../data/"
pyodide:
  resources:
    - "../../data/"

It's unclear how I should reference that data folder. Is it relative to where my *.qmd file sits relative to the root of the quarto directory. This is why I use ../../data, as my file sits in <root>/docs/demo. This relative location in gh-pages artifacts is the same. I have played around with this, but to no avail. I notice that if I include these resource references, then the rendered document somehow doesn't resolve. If I remove them, it at least resolves, although I'm not able to access the data from a cell.

The other thing of note is that the data folder is empty on the main branch. It only gets populated as part of a pre-render script.

Could you offer some guidance on how I should be doing this correctly? Should the data folder then be accessible within a pyodide cell under some web_user folder? Would be great to get a better understanding of how this works. Thanks.

georgestagg commented 2 months ago

Hi @Analect, thanks for reporting this! There were some bugs in the way the virtual filesystems structure was built as the WebAssembly engine loads. Sorry about that! I've made some commits to try and correct the problems.


The way this is supposed to work is that when written to the Wasm filesystem, the parent subdirectory structure should be collapsed (i.e. no leading '../../..'), but the remaining file structure (e.g. 'data/...') remains.

In your case, you should first update Quarto Live to the latest development build to get the changes I've just made:

$ quarto add r-wasm/quarto-live

Then, in your {pyodide} block, you would read your files from the data subdirectory, which should now exist in the Pyodide working directory:

print(listdir("data"))
pd.read_csv('data/core_meta-data.csv')

Please let me know if this is not the case, as it means some more work is still required.


As far as your document YAML header goes, since you already load the data directory into your resources in _quarto.yml, you likely don't need to include the top-level resources key again. You should only need the one under the pyodide key which tells Quarto Live to make those particular resources available in the VFS:

pyodide:
  resources:
    - "../../data/"
Analect commented 2 months ago

@georgestagg ... thanks for that fix. It seems to be working better, per here. Cells are set to be activated manually. Still working through things with another collaborator around why connecting to the embedded graph database is not working. Will close this for now.