quarto-ext / shinylive

Quarto extension to embed Shinylive for Python applications
https://quarto-ext.github.io/shinylive/
MIT License
144 stars 8 forks source link

Raise error if `embed-resources: true` #12

Open muellerzr opened 1 year ago

muellerzr commented 1 year ago

Hi all!

Love the project :)

Hit a fun snag when I was getting a recent blog up and going where if embed-resources:true is set in my _quarto.yml, shinylive just won't render!

Minimal repr:

YAML:

project:
  type: website
format:
  html:
    css: styles.css
    embed-resources: true

somepage.qmd:

# A test

```{shinylive-python}
#| components: [editor, terminal]

def hello_world():
  "Something that prints Hello World"
  return "Hello World"

hello_world()


Result:
![image](https://github.com/quarto-ext/shinylive/assets/7831895/25fa9e44-bd71-41c8-a916-8aee0c891933)

Whereas normally I'd expect a shinylive app to be there!

Note that if I *don't* have `embed-resources`, it works fine :) Thanks!
wch commented 1 year ago

I haven't taken a look at how the output, but there's a lot of dynamically loaded stuff (wasm files, wheels, and others), and unfortunately, I don't think it will be possible to embed all those resources into the page -- I don't even know how quarto would be able to know about them, since some of the files (like wheels) are only known about at run time.

muellerzr commented 1 year ago

No worries. Could it be possible to raise an error if that's being set?

wch commented 1 year ago

I think that probably can be done, but I'll have do a bit of investigation to figure out how.

muellerzr commented 1 year ago

That'd be great if so, otherwise this issue also can act as a quick find for folks that try the same thing 😅

mcaruel commented 11 months ago

I recommend writing this as a prerequisite in the Readme file

matthewt-assurity commented 2 months ago

Any updates on this?

I'm trying to do the exact same thing, but as @wch said above , it sounds like it's not possible to create a completely self-contained HTML file that contains a shinylive codeblock?

The idea of raising an error sounds good, then it at least gives the user some more information that it won't work.

gadenbuie commented 2 months ago

It turns out that shinylive's Lua filter won't have access to the embed-resources option and therefore we can't detect that embed-resources: true is set to be able to throw an error. Quarto may add that to the metadata it passes to the Lua filters in the future, in which case we can throw early.

In the mean time, I've come up with a hacky but workable solution. When shinylive is used with embed-resources: true, the original code chunk is shown in full instead of the shinylive app.

So we can add some warning code to the top of the code chunk that only shows up when shinylive fails to load. The top of the code chunk always contains the chunk options, so we need to use the same syntax:

#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.

Hopefully this gives enough information to the person with the broken output to figure out what's going wrong.