0

I create a html in plotly and want to upload it to azure blob storage.

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

How to write it in memory, so that I can upload it?

When using

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

I get the error on upload, that is not bytes.

When normally uploading html strings I use

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

Any suggestions how to do this?

EDIT:

I need to write it into memory, since I am using it in an azure function.

nanuuq
  • 169
  • 9

1 Answers1

0

I tried in my system able to upload the plotly html file I taken the sample dataframe

try with this code

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)

OUTPUT

enter image description here

ShrutiJoshi-MT
  • 1,622
  • 1
  • 4
  • 9
  • I edited my question: I am using Azure-functions, that is why I try not to save to disk, rather use the file in memory. I am not that familiar with that topic, that is why I used this for a parquet file in an azure function: `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)` – nanuuq Nov 17 '21 at 11:46
  • Instead of saving file locally ,You want to save in stream and upload it to blob .Correct me if I am wrong? – ShrutiJoshi-MT Nov 17 '21 at 11:59
  • Yes, that is what I am trying. – nanuuq Nov 17 '21 at 13:05
  • But as you mentioned `write_html ` saves the html file locally – ShrutiJoshi-MT Nov 17 '21 at 13:28
  • But writing it to StringIO goes without saving it locally. But how to convert it to bytes? – nanuuq Nov 18 '21 at 17:22
  • Please refer this SO Thread : https://stackoverflow.com/questions/55889474/convert-io-stringio-to-io-bytesio – ShrutiJoshi-MT Nov 22 '21 at 07:09
  • If the answer is helpful for you, you can accept it as answer( click on the check mark beside the answer to toggle it from greyed out to filled in.). This can be beneficial to other community members. Thank you – ShrutiJoshi-MT Nov 25 '21 at 11:01