---
tags: [gradio-custom-component, ]
title: gradio_headinjector
short_description: A Head injector for css and js
colorFrom: blue
colorTo: yellow
sdk: gradio
pinned: false
app_file: space.py
---
# `gradio_headinjector`
<a href="https://pypi.org/project/gradio_headinjector/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_headinjector"></a>
A head injector for css and js
## Installation
```bash
pip install gradio_headinjector
```
## Usage
```python
import gradio as gr
from gradio_headinjector import HeadInjector
def inject_assets():
"""
This function prepares the payload of CSS and JS code. It's called by the
demo.load() event listener when the Gradio app starts.
"""
# Inline code
css_code = "body { background-color: #f0f5ff !important; }"
js_code = "console.log('Hello from an inline JS string!');"
# Read from files
try:
with open("test.css", "r", encoding="utf-8") as f:
css_code += f.read() + "\n"
with open("test.js", "r", encoding="utf-8") as f:
js_code += f.read() + "\n"
except FileNotFoundError as e:
print(f"Warning: Could not read asset file: {e}")
return {"js": js_code, "css": css_code}
with gr.Blocks() as demo:
gr.Markdown("# Application with HeadInjector")
gr.Markdown("If the background is light blue and the buttons trigger an alert, it worked!")
# 1. Instantiate the component. It is empty at this point.
head_injector = HeadInjector()
with gr.Row():
gr.Button("Button 1")
gr.Button("Button 2")
gr.Textbox(label="Check your browser's developer console (F12) for JS messages.")
# 2. Use the `demo.load()` event to populate the component.
# This is the reliable pattern: `demo.load` runs the `inject_assets` function
# and sends its return value to the `head_injector` component.
demo.load(fn=inject_assets, inputs=None, outputs=[head_injector])
# Launch the application
if __name__ == '__main__':
demo.launch()
```
## `HeadInjector`
### Initialization
<table>
<thead>
<tr>
<th align="left">name</th>
<th align="left" style="width: 25%;">type</th>
<th align="left">default</th>
<th align="left">description</th>
</tr>
</thead>
<tbody></tbody></table>
### User function
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
- When used as an Input, the component only impacts the input signature of the user function.
- When used as an output, the component only impacts the return signature of the user function.
The code snippet below is accurate in cases where the component is used as both an input and an output.
```python
def predict(
value: Any
) -> typing.Optional[typing.Dict[str, str]][
typing.Dict[str, str][str, str], None
]:
return value
```
Raw data
{
"_id": null,
"home_page": null,
"name": "gradio-headinjector",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "gradio-custom-component, gradio-template-Fallback",
"author": null,
"author_email": "Eliseu Silva <elismasilva@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/7f/25/ac78267a033a55ffab6b05ae1b821ba1429b944174bcbe58b6852c722edf/gradio_headinjector-0.0.2.tar.gz",
"platform": null,
"description": "---\ntags: [gradio-custom-component, ]\ntitle: gradio_headinjector\nshort_description: A Head injector for css and js\ncolorFrom: blue\ncolorTo: yellow\nsdk: gradio\npinned: false\napp_file: space.py\n---\n\n# `gradio_headinjector`\n<a href=\"https://pypi.org/project/gradio_headinjector/\" target=\"_blank\"><img alt=\"PyPI - Version\" src=\"https://img.shields.io/pypi/v/gradio_headinjector\"></a> \n\nA head injector for css and js\n\n## Installation\n\n```bash\npip install gradio_headinjector\n```\n\n## Usage\n\n```python\nimport gradio as gr\nfrom gradio_headinjector import HeadInjector\n\ndef inject_assets():\n \"\"\"\n This function prepares the payload of CSS and JS code. It's called by the\n demo.load() event listener when the Gradio app starts.\n \"\"\"\n # Inline code\n css_code = \"body { background-color: #f0f5ff !important; }\"\n js_code = \"console.log('Hello from an inline JS string!');\"\n\n # Read from files\n try:\n with open(\"test.css\", \"r\", encoding=\"utf-8\") as f:\n css_code += f.read() + \"\\n\"\n with open(\"test.js\", \"r\", encoding=\"utf-8\") as f:\n js_code += f.read() + \"\\n\"\n except FileNotFoundError as e:\n print(f\"Warning: Could not read asset file: {e}\")\n\n return {\"js\": js_code, \"css\": css_code}\n\n\nwith gr.Blocks() as demo:\n gr.Markdown(\"# Application with HeadInjector\")\n gr.Markdown(\"If the background is light blue and the buttons trigger an alert, it worked!\")\n \n # 1. Instantiate the component. It is empty at this point.\n head_injector = HeadInjector()\n \n with gr.Row():\n gr.Button(\"Button 1\")\n gr.Button(\"Button 2\")\n\n gr.Textbox(label=\"Check your browser's developer console (F12) for JS messages.\")\n\n # 2. Use the `demo.load()` event to populate the component.\n # This is the reliable pattern: `demo.load` runs the `inject_assets` function\n # and sends its return value to the `head_injector` component.\n demo.load(fn=inject_assets, inputs=None, outputs=[head_injector])\n\n\n# Launch the application\nif __name__ == '__main__':\n demo.launch()\n```\n\n## `HeadInjector`\n\n### Initialization\n\n<table>\n<thead>\n<tr>\n<th align=\"left\">name</th>\n<th align=\"left\" style=\"width: 25%;\">type</th>\n<th align=\"left\">default</th>\n<th align=\"left\">description</th>\n</tr>\n</thead>\n<tbody></tbody></table>\n\n\n\n\n### User function\n\nThe impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).\n\n- When used as an Input, the component only impacts the input signature of the user function.\n- When used as an output, the component only impacts the return signature of the user function.\n\nThe code snippet below is accurate in cases where the component is used as both an input and an output.\n\n\n\n ```python\n def predict(\n value: Any\n ) -> typing.Optional[typing.Dict[str, str]][\n typing.Dict[str, str][str, str], None\n]:\n return value\n ```\n \n",
"bugtrack_url": null,
"license": null,
"summary": "A head injector for css and js",
"version": "0.0.2",
"project_urls": null,
"split_keywords": [
"gradio-custom-component",
" gradio-template-fallback"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "4a6935e3f2406414e99e8651c3c6af4a8cc3a49666ec0ad94ec54af01c9a43da",
"md5": "f42a075b7caf446d0bb8233aec18e67c",
"sha256": "75edf2485910b0004c695239227983f3bb7fe0952dbade642f5c104f6eed3ac9"
},
"downloads": -1,
"filename": "gradio_headinjector-0.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f42a075b7caf446d0bb8233aec18e67c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 55560,
"upload_time": "2025-07-29T00:45:36",
"upload_time_iso_8601": "2025-07-29T00:45:36.524858Z",
"url": "https://files.pythonhosted.org/packages/4a/69/35e3f2406414e99e8651c3c6af4a8cc3a49666ec0ad94ec54af01c9a43da/gradio_headinjector-0.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "7f25ac78267a033a55ffab6b05ae1b821ba1429b944174bcbe58b6852c722edf",
"md5": "5d33e4b4637d5c6f681b44526c61ba9a",
"sha256": "bf393f7f6dcc61ad45bddafdab4363b9ffd2e574f7670f69a254e124750be890"
},
"downloads": -1,
"filename": "gradio_headinjector-0.0.2.tar.gz",
"has_sig": false,
"md5_digest": "5d33e4b4637d5c6f681b44526c61ba9a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 54605,
"upload_time": "2025-07-29T00:45:37",
"upload_time_iso_8601": "2025-07-29T00:45:37.878111Z",
"url": "https://files.pythonhosted.org/packages/7f/25/ac78267a033a55ffab6b05ae1b821ba1429b944174bcbe58b6852c722edf/gradio_headinjector-0.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-29 00:45:37",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "gradio-headinjector"
}