简体   繁体   English

将 base64 编码图像嵌入 Dash 数据表

[英]Embedding an base64 encoded image into Dash Data Table

I am hoping to embed an image as a data:URI into a Dash Datatable as in the example below.我希望将图像作为 data:URI 嵌入到 Dash 数据表中,如下例所示。 I borrowed the embedding from this post .我从这篇文章中借用了嵌入。 I am creating the images on the fly using another library not included in the code below.我正在使用下面代码中未包含的另一个库动态创建图像。 When I run the Dash server using当我使用运行 Dash 服务器时

python app.py

I get a data.table with the encoded string and not an image.我得到一个带有编码字符串而不是图像的 data.table。 I thought using the 'presentation': 'markdown' (in the columns section of the dash documentation ) would work but unfortunately not.我认为使用 'presentation': 'markdown'(在破折号文档的列部分)会起作用,但不幸的是没有。

I also tried wrapping the url in a dash html.Img component but that did not seem to work either.我还尝试将 url 包装在破折号 html.Img 组件中,但这似乎也不起作用。

Any help on this would be appreciated!对此有任何帮助将不胜感激! Thank you谢谢

import dash
import dash_table
import pandas as pd


url1 = '![Hello World](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAAAUCAAAAAAVAxSkAAABrUlEQVQ4y+3TPUvDQBgH8OdDOGa+oUMgk2MpdHIIgpSUiqC0OKirgxYX8QVFRQRpBRF8KShqLbgIYkUEteCgFVuqUEVxEIkvJFhae3m8S2KbSkcFBw9yHP88+eXucgH8kQZ/jSm4VDaIy9RKCpKac9NKgU4uEJNwhHhK3qvPBVO8rxRWmFXPF+NSM1KVMbwriAMwhDgVcrxeMZm85GR0PhvGJAAmyozJsbsxgNEir4iEjIK0SYqGd8sOR3rJAGN2BCEkOxhxMhpd8Mk0CXtZacxi1hr20mI/rzgnxayoidevcGuHXTC/q6QuYSMt1jC+gBIiMg12v2vb5NlklChiWnhmFZpwvxDGzuUzV8kOg+N8UUvNBp64vy9q3UN7gDXhwWLY2nMC3zRDibfsY7wjEkY79CdMZhrxSqqzxf4ZRPXwzWJirMicDa5KwiPeARygHXKNMQHEy3rMopDR20XNZGbJzUtrwDC/KshlLDWyqdmhxZzCsdYmf2fWZPoxCEDyfIvdtNQH0PRkH6Q51g8rFO3Qzxh2LbItcDCOpmuOsV7ntNaERe3v/lP/zO8yn4N+yNPrekmPAAAAAElFTkSuQmCC)'

data = [['Item 1', url1], ['Item 2', url1]]

# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Image']) 

app = dash.Dash(__name__)


app.layout = dash_table.DataTable(
    id='table',
    columns=[{"name": 'Name', "id": 'Name'},
    {
            'id': 'Image',
            'name': 'Image',
            'presentation': 'markdown',
        },
    ],
    #fixed_columns={ 'headers': True, 'data': 1 },
    data=df.to_dict('records'),

)

if __name__ == '__main__':
    app.run_server(debug=True)

Looks like adding an assets folder locally and dumping image files in it works with respect to display看起来像在本地添加资产文件夹并在其中转储图像文件与显示有关

If I replace the url1 line in the above code with something like the following:如果我将上面代码中的 url1 行替换为如下所示:

url1 = '![myImage-1](assets/test.png)'

and make sure I add the location of the "assets_folder" in the app line并确保我在应用程序行中添加了“assets_folder”的位置

app = dash.Dash(__name__, assets_folder='assets')

an image will display in each row of the table.图像将显示在表格的每一行中。 Ideally the data uri would work and I would prefer that approach, but I figured I would post this for now as a potential answer.理想情况下,数据 uri 会起作用,我更喜欢这种方法,但我想我现在会将此作为潜在答案发布。

For dash==1.21.0 , the markdown_options parameter can fix this problem if you specify the image with an HTML tag.对于dash==1.21.0 ,如果您使用 HTML 标记指定图像,则markdown_options参数可以解决此问题。 Unfortunately, this solution also creates an XSS vulnerability according to the DataTable docs .不幸的是,根据DataTable 文档,该解决方案还创建了一个 XSS 漏洞。

Code:代码:

import dash
import dash_table

# base64 image string
img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAAAUCAAAAAAVAxSkAAABrUlEQVQ4y+3TPUvDQBgH8OdDOGa+oUMgk2MpdHIIgpSUiqC0OKirgxYX8QVFRQRpBRF8KShqLbgIYkUEteCgFVuqUEVxEIkvJFhae3m8S2KbSkcFBw9yHP88+eXucgH8kQZ/jSm4VDaIy9RKCpKac9NKgU4uEJNwhHhK3qvPBVO8rxRWmFXPF+NSM1KVMbwriAMwhDgVcrxeMZm85GR0PhvGJAAmyozJsbsxgNEir4iEjIK0SYqGd8sOR3rJAGN2BCEkOxhxMhpd8Mk0CXtZacxi1hr20mI/rzgnxayoidevcGuHXTC/q6QuYSMt1jC+gBIiMg12v2vb5NlklChiWnhmFZpwvxDGzuUzV8kOg+N8UUvNBp64vy9q3UN7gDXhwWLY2nMC3zRDibfsY7wjEkY79CdMZhrxSqqzxf4ZRPXwzWJirMicDa5KwiPeARygHXKNMQHEy3rMopDR20XNZGbJzUtrwDC/KshlLDWyqdmhxZzCsdYmf2fWZPoxCEDyfIvdtNQH0PRkH6Q51g8rFO3Qzxh2LbItcDCOpmuOsV7ntNaERe3v/lP/zO8yn4N+yNPrekmPAAAAAElFTkSuQmCC"

# add html
img = f"<img src={img}>"

# make app
data = [{"Image": img, "Text": "test"}]

app = dash.Dash(__name__)
app.layout = dash_table.DataTable(
    columns=[
        {"id": name, "name": name, "presentation": "markdown"}
        for name in data[0]
    ],
    data=data,
    markdown_options={"html": True}  # dangerous but works
)

if __name__ == "__main__":
    app.run_server(debug=True)

Dash app screenshot:破折号应用截图:

Dash 应用截图

Another alternative is to use bootstrap:apparently it's much easier to display an image in a table with dbc rather than dash_table .另一种选择是使用引导程序:显然,使用dbc而不是dash_table在表中显示图像要容易得多

(NOTE: Please take into consideration that this approach leaves you open to an XSS vulnerability) (注意:请考虑到这种方法会让您面临 XSS 漏洞)

I've had the same problem which I've now managed to solve (One thing to note, I'm doing this using the Django framework, but it shouldn't matter).我遇到了同样的问题,现在我已经设法解决了(需要注意的一件事,我正在使用 Django 框架来做这件事,但这无关紧要)。

My approach was to create a list of dicts (in a variable called columns_format , you can call it whatever you want) where I defined the column names and id, and in the dict (where the id is equal to 'icons' , again yours can be called something different) is where you must first insert the parameter presentation and equal it to 'markdown' .我的方法是创建一个字典列表(在一个名为columns_format的变量中,您可以随意调用它)我在其中定义了列名和 id,并在字典中(其中id等于'icons' ,再次是你的可以称为不同的东西)是您必须首先插入参数表示并将其等于'markdown' 的地方。

columns_format = [
    dict(id = 'icons', name = '', presentation='markdown'),
    dict(id = 'symbol', name='Symbol'),
    dict(id = 'name', name='Coins'),

Once you have specified your columns in the columns_format variable you must then set the markdown_options parameter to 'html': True , and then set the columns parameter equal to the columns_format variable created earlier.columns_format变量中指定列后,您必须将markdown_options参数设置为'html': True ,然后将columns参数设置为等于之前创建的columns_format变量。

app.layout = dash_table.DataTable(
    markdown_options = {
        'html': True
    },
    data = coins_df,
    columns = columns_format,
)

What must happen (incase you are referencing an image from a link is to write it in a html format as below:必须发生的事情(如果您从链接引用图像是将其写入 html 格式,如下所示:

<img src="https://cdn.coinranking.com/bOabBYkcX/bitcoin_btc.svg" style="height: 30px; width:30px;"/>

So in each column where you want the images to appear must be in the above html format (height and width optional but recommended)所以在你希望图片出现的每一列中必须是上面的 html 格式(高度和宽度可选但推荐)

My dash DataTable below has the icons/images of Crypto Currency besides their symbol referencing a url for the location of the icons/images.我下面的破折号数据表有加密货币的图标/图像,除了它们的符号引用 url 作为图标/图像的位置。

破折号数据表中的图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM