Skip to content

Fix issue with head param when adding more than one script tag #6639

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

Merged
merged 8 commits into from
Dec 12, 2023
Merged

Conversation

dawoodkhan82
Copy link
Collaborator

Description

Fixes issue with head param when adding more than one script tag.

Closes: #6426

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Dec 3, 2023

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
Visual tests all good! Build review
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/9c28267ffd0b06be3aba3d957a04ae6a16d7d2b2/gradio-4.8.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@9c28267ffd0b06be3aba3d957a04ae6a16d7d2b2#subdirectory=client/python"

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Dec 3, 2023

🦄 change detected

This Pull Request includes changes to the following packages.

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

With the following changelog entry.

Fix issue with head param when adding more than one script tag

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.

@abidlabs
Copy link
Member

abidlabs commented Dec 4, 2023

@dawoodkhan82 I can confirm that both scripts are added now. However, they are not actually run. For example, in this app:

import gradio as gr

head = """
<script>console.log('hello')</script>
<script>console.log('world')</script>
"""

with gr.Blocks(head=head) as demo:
    pass

demo.launch()

the console log is empty. Is there a way to force the scripts to run after they have been added to the page?

@dawoodkhan82
Copy link
Collaborator Author

@abidlabs Trying to debug, not sure what changed.

@abidlabs
Copy link
Member

abidlabs commented Dec 8, 2023

@dawoodkhan82 just fyi I think this was the case before as well -- the script would not get executed

@abidlabs
Copy link
Member

abidlabs commented Dec 8, 2023

Based on this part of the original issue:

Additionally, in this case it seems like the script tag is added to the head section after the page already loads, so it never gets executed? The console output is empty.

@pngwn
Copy link
Member

pngwn commented Dec 11, 2023

The script being added later shouldn't stop it from being executed.

@dawoodkhan82
Copy link
Collaborator Author

Fixed, realized I was pushing to the wrong branch...

@dawoodkhan82
Copy link
Collaborator Author

Also, now it works when trying to call an external js file:

import gradio as gr
import os

path = os.path.join(os.path.dirname(__file__), "test.js")
head = """
<script type="text/javascript" src={}></script>
<script>console.log('hello')</script>
""".format(path)

with gr.Blocks(head=head) as demo:
    pass

demo.launch()

@freddyaboulton
Copy link
Collaborator

I took a pass and I wasn't able to get the external script loading to work. @dawoodkhan82 is looking into it! 🙏

@abidlabs
Copy link
Member

Beautiful! Works for me.

The original code I shared works as well as loading js from an external PR.

Here's what I tried:

import gradio as gr
import os

head = """
<script type="text/javascript" src="/file=test.js"></script>
<script>console.log('hello')</script>
""".format(path)

with gr.Blocks(head=head) as demo:
    pass

demo.launch(allowed_paths=["test.js"])

Here's test.js:

console.log('world');

Copy link
Member

@abidlabs abidlabs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome stuff @dawoodkhan82

@dawoodkhan82
Copy link
Collaborator Author

@abidlabs Thanks for the review!
@freddyaboulton does adding demo.launch(allowed_paths=["test.js"]) to the demo fix the issue you were seeing?

@abidlabs abidlabs merged commit 9a6ff70 into main Dec 12, 2023
@abidlabs abidlabs deleted the head branch December 12, 2023 22:00
@abidlabs
Copy link
Member

merging ahead of the release

@pngwn pngwn mentioned this pull request Dec 12, 2023
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.

gr.Blocks head argument not working as intended
5 participants