Skip to content

Custom libraries in head of gr.HTML#12990

Open
aliabid94 wants to merge 4 commits intoadd_watch_to_custom_htmlfrom
add_head_to_gr_html
Open

Custom libraries in head of gr.HTML#12990
aliabid94 wants to merge 4 commits intoadd_watch_to_custom_htmlfrom
add_head_to_gr_html

Conversation

@aliabid94
Copy link
Collaborator

Add head= support to gr.HTML to support third party libraries.

    head_html = gr.HTML(
        value=[30, 70, 45, 90, 60],
        html_template="""
        <canvas id="head-chart" width="300" height="200"></canvas>
        """,
        js_on_load="""
        const canvas = element.querySelector('#head-chart');
        new Chart(canvas, {
            type: 'bar',
            data: {
                labels: props.value.map((_, i) => 'Item ' + (i + 1)),
                datasets: [{
                    label: 'Values',
                    data: props.value,
                    backgroundColor: 'rgba(99, 132, 255, 0.5)',
                }]
            },
            options: { responsive: false }
        });
        """,
        head='<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.7/dist/chart.umd.min.js"></script>',
        elem_id="head_demo",
    )

};
}

function plain_value(v: any): any {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Passing value to the chart.js example in super_html was causing some conflict with the proxying we do in gr.html to the props object, so now we do this deep copying to fix this bug.

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Mar 10, 2026

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview

Install Gradio from this PR

pip install https://gradio-pypi-previews.s3.amazonaws.com/8c0a6d239cd70a8dddc85da2a5166c63b59d0ca7/gradio-6.9.0-py3-none-any.whl

Install Gradio Python Client from this PR

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

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/8c0a6d239cd70a8dddc85da2a5166c63b59d0ca7/gradio-client-2.1.0.tgz

@aliabid94 aliabid94 changed the title changes Custom libraries in head of gr.HTML Mar 10, 2026
Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

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

LGTM but I think we should use $state.snapshot instead of plain_props/plain_value. It is the svelte 5 idiomatic way to turn the proxy object into a plain value and we shouldn't need to re-implement it ourselves.

Ty for updating the skills and the docs!

Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

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

Why don't we use <svelte:head > here? It will add / remove the elements correctly when the component is added/ removed, ensuring we don't have any memory leaks and is a bit easier to reason about in code.

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.

4 participants