-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
[HTML] how to load local image by html output #884
Comments
Hi @Olvi73 thanks for creating this issue! We are going to take a look at it soon |
Hi @Olvi73, you can access relative files by adding the prefix So if the code to your Gradio app is in a file called Then the code in import gradio as gr
title = "test"
def inference(text):
html = (
"<div >"
"<img src='file/lion.jpg' alt='image One'>"
+ "</div>"
)
return html
gr.Interface(
inference,
gr.inputs.Textbox(placeholder="Enter sentence here..."),
outputs=["html"],
title=title,
allow_flagging="never",
).launch(enable_queue=True) Note that for security reasons, any file that is included in your app must be in the same directory or in a child directory of |
Can i add a logo picture near title text? |
SAME QUESTIONS LET ME KNOW IF YOU FOUND ANY SOLUTION YET! THANKS |
. |
Hello, this issue still seems to exist for me, I get a 403 forbidden error. According to your response @abidlabs, it should work if I add the "file/" prefix and if both files are in the same directory, which is the case in my project. Please help. File structure: Code:
Error Message from Firefox console:
|
Hi @tlabarta this is an old thread which predates the file access rules of gradio 4.0. You need to specify the file paths you want to show with |
@freddyaboulton Thank you for your reply. Unfortunately, this has not solved my issue. No matter if I try
I get:
|
Hi @tlabarta , the route is |
@freddyaboulton This worked now, thanks a lot! |
Thanks so much for this comment here. Literally saved me a lot of time and headache! |
It's on my plate to make this process less confusing but glad this thread is useful in the meantime! |
Thank you very much for answering my questions and it worked in my project. -rootpath According to your opinion, the readme.md document must use the following methods to display pic.png on the webpage: |
I have the same issue and none of the above answers worked! I have a simple Gradio UI and need a header loading a local image which is next to the .py file. It can be loaded using the import gradio as gr
def greet(name):
return f"Hello {name}!"
image_path = "flytask_logo.png"
with gr.Blocks() as demo:
html_header = f"""
<div style="text-align: center;">
<img src="{image_path}" alt="Header Image" width="200" height="100">
</div>
"""
gr.HTML(html_header)
name_input = gr.Textbox(label="Enter your name:")
submit_button = gr.Button("Submit")
output = gr.Textbox(label="Greeting:")
submit_button.click(fn=greet, inputs=name_input, outputs=output)
demo.launch() I also tested with |
Which version of gradio does this solution with? Thx |
I had no luck so far adding an image to the gradio interface for my chatbot , with the below method. Looking for a solution for this problem.. Path to the chatbot image in the Colab project directorychatbot_image_path = "/content/chatbot_image.jpg" Create a custom CSS stringcustom_css = """ Create Gradio interfaceiface = gr.Interface(
) |
The issue you have is related to the image path and one possible solution is using gr.set_static_paths(paths=["static/images/"])
image_path = "static/images/logo.png"
gr.HTML(f"""<img src="/file={image_path}" width="100" height="100">""") Note: you would need to create a For more details check this SO post out. |
@agn-7 Your solution seems to work with 'gr.Markdown' method.
Thanks a lot @agn-7 , @freddyaboulton for your help. |
This works for me in the sense that I can request the URL: http://host:port/file=static_allowed_path_to_file but this does not render as an image (using the |
Not working anymore. |
Is there a simple workaround I could try? It’s a bit frustrating that my app isn’t showing the right logo anymore. Thanks so much for any help! |
@JSchmie
def load_css():
with open('styles.css', 'r') as file:
css_content = file.read()
return css_content
with gr.Blocks(css=load_css(), theme=theme) as demo:
gr.Image("./images/main-logo.png", elem_classes="logo-image", interactive=False, container=False, show_share_button=False, show_download_button=False, show_fullscreen_button=False, show_label=False)
.logo-image {
height: 70px;
position: absolute;
left: 10px;
} |
Hi @snapfast, thank you for your prompt response! I tested the solution you provided, but I found a quicker workaround in my case. It appears that For instance, the following code doesn't display the image as expected: import gradio as gr
with gr.Blocks() as demo:
gr.HTML("<img src='/file=./logo.svg' alt='image One'>")
demo.launch() However, switching to import gradio as gr
with gr.Blocks() as demo:
gr.HTML("<img src='/file=./logo.png' alt='image One'>")
demo.launch() Additionally, I noticed that I don't need to set @abidlabs, it would be extremely helpful if the documentation could include a brief guide on serving HTML resources, especially clarifying the behavior with different image formats and deployment scenarios. Thanks a lot again for your help, and I just want to add that I really enjoy working with Gradio. It's an amazing tool! |
Thanks @JSchmie for pointing that out. This has to do with how we serve svg files (for security reasons, we add a content disposition header which prevents it from being served inline). I'm looking into whether we can relax this for security reasons |
hey guys, I would like to add a solution by loading the photo: We can upload the photo to a server and just copy the photo link! I use https://picui.cn/ just upload our photo, and get a link, and then paste the link into src It just works as loading the local image Hope this way can help someone... Best regards |
We need to know for what versions of gradio it used to work for. I'm using gradio 5.4 and can't seem to get any local image to display. I'm using
There is no nested folders.. |
In Gradio 5, the route is /gradio_app/file= |
The And @abidlabs - did you just change the format of how to call the file in gradio version 5 to This option is not well documented for sure. I learned about it from this discussion as a solution suggested by @abidlabs to avoid needing to run fastapi alongside gradio to serve files. I understand there are some security concerns, yet if you allow using a file either by set_static_paths or If version 5.6 changes something significant that breaks old behavior, perhaps it a good idea to document this in the security guide. However, this option of exposing static files is sometimes important to communicate with other APIs that require files passed through a URL. I understand one can work around this by creating a specific end point to serve a file like in the echo example in this accessing a network request directly section in this page or create request objects that pass the image payload. However, it is so much simpler and so much like serving static files in flask and fastapi that one should consider allowing it in gradio. Hopefully you will fix it with future versions and make it work rather than deprecating the option. If you keep it, please consider enhancing the docs - I am happy to help if you need it if I know what you plan to do. |
Hi @Jacob-Barhak, this is working for me: import gradio as gr
with gr.Blocks() as demo:
gr.HTML("<img src='/gradio_api/file=cat.jpg'>")
demo.launch(allowed_paths=["cat.jpg"]) where |
For now, I've listed it in the breaking changes here: #9463 Feel free to open a PR to add it to a relevant sections in the docs @Jacob-Barhak and we are happy to review |
Ok @abidlabs The #9463 issue explains what is happening. During development and before deploying gradio I am testing it locally and this is probably the source of the problem. I am using So it seems you blocked the ability to run things locally for debugging and development. The code that exposes an image in a static file in 4.44.1 successfully is blocked in 5.6.0 and one can spend hours figuring out what is going on. I understand you have security concerns, yet can you at least leave a debug option that allows local development that allows accessing files on the localhost? Other platforms such as fastapi or flask allow this - some have a specific flag to mention one is debugging. I was confused for a long time why things are not working as written before I posted this issue and you explained it. Not sure what is the best solution here - for now may just use an earlier version for development - although this is not ideal... Thanks for the quick reply. |
Just a note - you made a typo on this. It's /gradio_api/file= |
Describe the bug
When I run the gradio code, it will
running on local URL: http://127.0.0.1:7860/
, in this way I can't use html code like this to load a imageor use absolute path
Reproduction
Screenshot
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: