Skip to content

load theme.css relatively instead of from config.root #11165

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

Closed

Conversation

sharon-wang
Copy link
Contributor

Description

To address #11164, here's a proposal to load theme.css relatively instead of from config.root, so that the link href is the expected value when the gradio server is accessed via a proxy server.

Closes: #11164

Current

<link rel="stylesheet" href="http://127.0.0.1:7860/theme.css?v=63194d3741d384f9f85db890247b6c0ef9e7abac0f297f40a15c59fe4baba916">

Other resources loaded in the head

<link rel="stylesheet" href="http://127.0.0.1:63454/assets/DownloadLink-CqD3Uu0l.css">

With Proposed Change

<link rel="stylesheet" href="./theme.css?v=63194d3741d384f9f85db890247b6c0ef9e7abac0f297f40a15c59fe4baba916">

@kuhnroyal
Copy link

Ran into this today, looking forward to this being released.
Would you mind also fixing the manifest.json path here https://github.com/gradio-app/gradio/blob/main/js/spa/index.html#L100?

@aliabid94
Copy link
Collaborator

aliabid94 commented May 12, 2025

I believe the reason we load the theme.css (and some other content) from config.root rather than relatively, is in the case of gradio app embeds.
Imagine http://hostsite.com/app is running gradio, and someone wants to embed this on their own site http://mirrorsite.com. Then on mirrorsite.com they can use the following code:

<script
	type="module"
	src="https://gradio.s3-us-west-2.amazonaws.com/5.22.0/gradio.js"
></script>

<gradio-app src="http://hostsite.com/app"></gradio-app>

And mirrorsite would load the app from http://hostsite.com/app, and we also want theme.css to load from http://hostsite.com/app/theme.css. If we were using relative imports instead as suggested in this PR, I believe this would not work.

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented May 12, 2025

🪼 branch checks and previews

Name Status URL
🦄 Changes detected! Details

@gradio-pr-bot
Copy link
Collaborator

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@self/spa patch
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

load theme.css relatively instead of from config.root

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@sharon-wang
Copy link
Contributor Author

@aliabid94 thanks for highlighting this, would you be able to point me to or create a sample app with this method of app embedding? I'd be happy to test this change with that configuration!

Would you mind also fixing the manifest.json path here https://github.com/gradio-app/gradio/blob/main/js/spa/index.html#L100?

@kuhnroyal happy to make this change too if this approach ends up working out for the app embed method @aliabid94 described!

This change resolves the theme.css loading issue I'm seeing when the gradio app is embedded via an iframe in a proxied environment. I have a standalone sample app setup in #11164, but in my iframe setup, the same issue occurs. It would be great to also confirm this wouldn't break the case you're describing.

@aliabid94
Copy link
Collaborator

Sorry for the late reply @sharon-wang !

If you run the following demo below, it works on main but does not work on this branch (after you rebuild the frontend). This demo uses your local gradio build for the frontend to locally embed the gradio app at https://opensound-solospeech.hf.space

from fastapi import FastAPI
import uvicorn
from fastapi.responses import HTMLResponse

app = FastAPI()

@app.get("/gradio/{path:path}")
def gradio_lib(path):
    import importlib.resources
    BUILD_PATH_LIB = importlib.resources.files("gradio").joinpath("templates/frontend/").as_posix()
    return HTMLResponse(
        open(f"{BUILD_PATH_LIB}/{path}").read(),
        media_type="text/javascript" if path.endswith(".js") else "text/css",
    )

@app.get("/")
def main():
    return HTMLResponse(
        """
        <html>
            <head>
                <title>Gradio Embed</title>
            </head>
            <body>
                <script type="module" src="/gradio/gradio.js"></script>
                <gradio-app src="https://opensound-solospeech.hf.space"></gradio-app>
            </body>
        </html>
        """
    )

if __name__ == "__main__":
    uvicorn.run(app=app,)

@abidlabs
Copy link
Member

This should be fixed via #11387, so I'll close this PR for now

@abidlabs abidlabs closed this Jun 12, 2025
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.

should the theme.css href be a relative path from the page url instead of relative to config.root?
5 participants