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

fix: support static files on pyodide / py.cafe under a prefix #1486

Merged

Conversation

maartenbreddels
Copy link
Contributor

When a shiny app is running under a prefix (root_path in ASGI terms), the static files are not served correctly under pyodide. This is because the ASGI path includes the root_path, and the root_path should be removed.
Starlette 0.33 and 0.34 however, did not set root_path correctly, and in those cases, we have to rely on route_path.

Related discussions:
encode/starlette#2400
encode/starlette#2361

A related fix we had in Solara:
widgetti/solara#413

But this fix also did not seem to work for our situation at https://py.cafe

I logged the output of the relevant entries in the scope dict, together with the starlette version:

starlette 0.32.0 {'path': '/strftime-min.js', 'root_path': '/_app/lib/strftime-0.9.2'}
starlette 0.33.0 {'path': '/_app/lib/strftime-0.9.2/strftime-min.js', 'root_path': '', 'route_root_path': '/lib/strftime-0.9.2', 'route_path': '/strftime-min.js'}
starlette 0.34.0 {'path': '/_app/lib/strftime-0.9.2/strftime-min.js', 'root_path': '', 'route_root_path': '/lib/strftime-0.9.2', 'route_path': '/strftime-min.js'}
starlette 0.35.0 {'path': '/_app/lib/strftime-0.9.2/strftime-min.js', 'root_path': '/_app/lib/strftime-0.9.2'}
starlette 0.36.0 {'path': '/_app/lib/strftime-0.9.2/strftime-min.js', 'root_path': '/_app/lib/strftime-0.9.2'}
starlette 0.37.2 {'path': '/_app/lib/strftime-0.9.2/strftime-min.js', 'root_path': '/_app/lib/strftime-0.9.2'}

This was using a similar situation as on py.cafe:

....
routes = [
    Mount('/static', app=app_static),
    Mount('/_app', app=app_shiny)
]

app = Starlette(routes=routes)

Which led me to the following fix, making shiny work under pyodide in combination with a prefix with the above mentioned versions of starlette.

When a shiny app is running under a prefix (root_path in ASGI terms),
the static files are not served correctly under pyodide.
This is because the ASGI path includes the root_path, and the root_path
should be removed.
Starlette 0.33 and 0.34 however, did not set root_path correctly,
and in those cases, we have to rely on route_path.

Related discussions:
 encode/starlette#2400
 encode/starlette#2361

A related fix we had in Solara:
 widgetti/solara#413

But this fix also did not seem to work for our situation at https://py.cafe

I logged the output of the relevant entries in the scope dict, together
with the starlette version:

```
starlette 0.32.0 {'path': '/strftime-min.js', 'root_path': '/_app/lib/strftime-0.9.2'}
starlette 0.33.0 {'path': '/_app/lib/strftime-0.9.2/strftime-min.js', 'root_path': '', 'route_root_path': '/lib/strftime-0.9.2', 'route_path': '/strftime-min.js'}
starlette 0.34.0 {'path': '/_app/lib/strftime-0.9.2/strftime-min.js', 'root_path': '', 'route_root_path': '/lib/strftime-0.9.2', 'route_path': '/strftime-min.js'}
starlette 0.35.0 {'path': '/_app/lib/strftime-0.9.2/strftime-min.js', 'root_path': '/_app/lib/strftime-0.9.2'}
starlette 0.36.0 {'path': '/_app/lib/strftime-0.9.2/strftime-min.js', 'root_path': '/_app/lib/strftime-0.9.2'}
starlette 0.37.2 {'path': '/_app/lib/strftime-0.9.2/strftime-min.js', 'root_path': '/_app/lib/strftime-0.9.2'}
```

This was using a similar situation as on py.cafe:
```python
....
routes = [
    Mount('/static', app=app_static),
    Mount('/_app', app=app_shiny)
]

app = Starlette(routes=routes)
```

Which led me to the following fix, making shiny work under pyodide
in combination with a prefix with the above mentioned versions of starlette.
@wch wch added this pull request to the merge queue Jun 28, 2024
@wch
Copy link
Collaborator

wch commented Jun 28, 2024

Thanks for the PR!

I had previously tried updating the version of starlette included with shinylive but that caused shiny to break, and this change appears to fix that issue.

Merged via the queue into posit-dev:main with commit 8e3ce87 Jun 28, 2024
31 checks passed
schloerke added a commit to machow/py-shiny that referenced this pull request Jul 2, 2024
* main:
  fix(tests): dynamically determine the path to the shiny app (posit-dev#1485)
  tests(deploys): use a stable version of html tools instead of main branch (posit-dev#1483)
  feat(data frame): Support basic cell styling (posit-dev#1475)
  fix: support static files on pyodide / py.cafe under a prefix (posit-dev#1486)
  feat: Dynamic theming (posit-dev#1358)
  Add return type for `_task()` (posit-dev#1484)
  tests(controls): Change API from controls to controller (posit-dev#1481)
  fix(docs): Update path to reflect correct one (posit-dev#1478)
  docs(testing): Add quarto page for testing (posit-dev#1461)
  fix(test): Remove unused testrail reporting from nightly builds (posit-dev#1476)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants