【问题标题】:Uploading a plotly html to azure blob: change string to bytes将绘图 html 上传到 azure blob:将字符串更改为字节
【发布时间】:2021-12-27 20:43:57
【问题描述】:

我在 plotly 中创建了一个 html,并希望将其上传到 azure blob 存储。

fig.write_html("C:/test.html")

如何写在内存中,以便上传?

使用时

html_file = io.StringIO()
fig.write_html(html_file)

上传时出现错误,不是字节。

正常上传我使用的html字符串时

blob_client.upload_blob(html.encode('utf-8') ,blob_type="BlockBlob", overwrite=True, content_settings=ContentSettings(content_type='text/html'))

有什么建议吗?

编辑:

我需要将它写入内存,因为我在天蓝色函数中使用它。

【问题讨论】:

    标签: python azure-functions plotly azure-blob-storage


    【解决方案1】:

    我尝试在我的系统中上传我获取示例数据框的 plotly html 文件

    试试这段代码

    import plotly.express as px
    import plotly.io as pio
    import pandas as pd
    from azure.storage.blob import BlobServiceClient
    
    data = {'Product': ['Desktop Computer','Tablet','Printer','Laptop'],
            'Price': [850,200,150,1300]
            }
    
    df = pd.DataFrame(data, columns= ['Product', 'Price'])
    fig = px.bar(df, x='Product', y='Price')
    pio.write_html(fig, file='output.html')
    
    blob_service_client = BlobServiceClient.from_connection_string("Connection string")
    container_client=blob_service_client.get_container_client("test")
    path="C:\\Users \\output.html"
    
    with open(path, "rb") as data:
        blob_clientnew = container_client.get_blob_client("output.html")
        blob_clientnew.upload_blob(data)
    

    输出

    【讨论】:

    • 我编辑了我的问题:我正在使用 Azure 功能,这就是为什么我尽量不保存到磁盘,而是使用内存中的文件。我对那个主题不太熟悉,这就是为什么我将它用于天蓝色函数中的镶木地板文件:parquet_file = io.BytesIO() df_all.to_parquet(parquet_file, engine='pyarrow') parquet_file.seek(0) # change the stream position back to the beginning after writing blob_client.upload_blob(data=parquet_file, overwrite=True)
    • 不是在本地保存文件,而是要保存在流中并将其上传到 blob。如果我错了,请纠正我?
    • 是的,这就是我正在尝试的。
    • 但是正如你提到的write_html 将html文件保存在本地
    • 但是将其写入 StringIO 并没有将其保存在本地。但是如何将其转换为字节呢?
    猜你喜欢
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 2015-10-16
    • 2018-12-28
    • 1970-01-01
    相关资源
    最近更新 更多