【Gradio】gr.Markdown 不显示本地图片的问题

问题描述

使用 Gradio (版本4.39.0) 开发界面时,遇到 gr.Markdown 不显示本地图片的问题。

![图片](resource/test.png "picture")

原因分析:

参考https://github.com/gradio-app/gradio/issues/2682、https://github.com/gradio-app/gradio/issues/1997,作者大大解释为:

This issue happens because the web app doesn’t know how to resolve the path to the image that you provided. (web 程序无法解析图片路径)

但使用file/前缀不能解决问题(图片依旧 broken),例如:

![图片](file/resource/test.png "picture")

解决方案:

  1. launch增加allowed_paths=['./']参数,即允许 Gradio 程序访问程序路径下的所有文件,例如:
    demo.launch(server_name='xxx', server_port=xxx, allowed_paths=['./'])
    
  2. markdown的图片本地链接使用/file=前缀(后接相对路径),例如:
    ![图片](/file=resource/test.png "picture")
    

完美解决问题,mark一下。

你可能感兴趣的:(个人开发随记,python,ui,前端,开源)