Displaying image from url in shiny python

Hi,

How would I display an image from a URL in python shiny. I am trying to build where a user can supply a url of an image to display it within my app

I tried the approach of downloading the image directly from the url in the app below, as inspired by the documentation but the app gets reloaded constantly due to watchfiles so I am wondering if there is a better approach rather than turning off watch files

Thanks

Iain

Thanks

Iain

       from shiny import App, Inputs, Outputs, Session, render, ui
      from shiny.types import ImgData
      import requests

      app_ui = ui.page_fluid(ui.output_image("image"))

  def server(input: Inputs, output: Outputs, session: Session):

       @output
      @render.image
     def image():
          from pathlib import Path
          img_url = "https://sample-videos.com/img/Sample-png-image-100kb.png"
          response = requests.get(img_url)
    
        if response.status_code:
             fp = open('my_image.png', 'wb')
             fp.write(response.content)
             fp.close()

       dir = Path(__file__).resolve().parent
       img: ImgData = {"src": str(dir / "my_image.png"), "width": "100px"}
    return img


 app = App(app_ui, server)

Have you considered using ui.tags.img in your code?

ui.tags.img(src=url, height="100%", width="100%"),
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.