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

Add script_to_iframe function #6123

Open
MarcSkovMadsen opened this issue Dec 26, 2023 · 0 comments
Open

Add script_to_iframe function #6123

MarcSkovMadsen opened this issue Dec 26, 2023 · 0 comments
Labels
type: enhancement Minor feature or improvement to an existing feature
Milestone

Comments

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Dec 26, 2023

Sometimes you would like to embed a app pyodide/ pyscript app in another html document. And maybe you don't want or cannot host the panel convert assets externally.

One use case is plugins/ filters/ extension to document frameworks like Sphinx, mkDocs or Quarto.

Here you would need code like the below

import html
from io import StringIO
import sys  

from panel.io.convert import script_to_html
from panel.io.resources import set_resource_mode

ALLOW="accelerometer;autoplay;camera;document-domain;encrypted-media;fullscreen;gamepad;geolocation;gyroscope;layout-animations;legacy-image-formats;microphone;oversized-images;payment;publickey-credentials-get;speaker-selection;sync-xhr;unoptimized-images;unsized-media;screen-wake-lock;web-share;xr-spatial-tracking"

def script_to_iframe(code: str, runtime: str="pyscript")->str:
    """Converts the code to a valid iframe for embedding"""
    if runtime=="pyodide-worker":
        # The pyodide-worker runtime generates an both a html and a js file
        raise ValueError("pyodide-worker runtime is not supported!")
    with set_resource_mode('cdn'):
        ht, _ = script_to_html( 
            StringIO(script), runtime=runtime
        )
    ht = html.escape(ht)
    return f"""<iframe frameborder="0" srcdoc="{ht}" class="panel-app" onload="resizePanelApp(this)" allow="{ALLOW}"></iframe>"""

Its tedious to have to develop and maintain this in many settings.

It would be nice if Panel included a ready to use script_to_iframe function.

Additional Context

  • the pyodide-worker is not supported by the above implementation. The external js worker would need to be hosted externally by panel (without the code) or embedded in the html document generated via a blob url. See https://gist.github.com/nolanlawson/23eff93d27ad09ff44b7e4d56ffd1d54. The pyoide-worker runtime loading looks much nicer than the pyscript loading. Thus I would prefer to be able to use that one.
  • The limitation of the script_to_iframe is that it does not work well if you want to embed many panel apps in a page because each iframe loads all the resources etc. Shiny-live solves this by loading once outside the iframe and then setting up a communication channel between parent window and iframe.
  • I think the line with set_resource_mode('cdn') is a hack and some refactoring of Panel code should be done so that this can be avoided.
@MarcSkovMadsen MarcSkovMadsen added the TRIAGE Default label for untriaged issues label Dec 26, 2023
@philippjfr philippjfr added type: enhancement Minor feature or improvement to an existing feature and removed TRIAGE Default label for untriaged issues labels Dec 26, 2023
@philippjfr philippjfr added this to the v1.4.0 milestone Dec 26, 2023
@philippjfr philippjfr modified the milestones: v1.4.0, v1.4.x Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Minor feature or improvement to an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants