Search code examples
pythonpython-3.xazureazure-functionsazure-storage

Is there a way to write an html file (plotly) to azure storage blob with python?


I am stuck trying to find a way to upload an html file to my azure storage blob in python. I have tried creating a temporary folder, saving the html file and from there uploading it. I have also tried just directly using my output binding to set it (which doesn't work).

I have had success uploading a JPEG, but I need the HTML file specifically to work as it's user dynamic (it's created with the graph / plotly library).

    def main(mytimer: func.TimerRequest,outputblob: func.Out[bytes],inputblob: bytes,mypictureblob: func.Out[func.InputStream]): 



    def return_graph(final_difference):
    final_difference = final_difference
    fig = go.Figure()
    fig.add_trace(go.Scatter(y=final_difference['Expected Supply'],x = final_difference.index,hoverinfo='x+y',
                            mode = 'lines',
                            fill='tozeroy'))

    fig.update_layout(
        
        title="24 Month Supply and Demand - Daily Peak Hour",
        xaxis_title="Date",
        yaxis_title="MW Surplus",
        legend_title="",
        font=dict(
            family="Courier New, monospace",
            size=15,
            color="green"
        ))

    fig.update_layout(
        title={
            'text': "24 Month Supply and Demand - Daily Peak Hour",
            'y':0.9,
            'x':0.5,
            'xanchor': 'center',
            'yanchor': 'top'})


    dir_path = tempfile.gettempdir()
    asf = fig.write_html(file = dir_path  + '/ss.html')
    # This is just the naïve method to just hopefully cram it in the azure storage 
    #mypictureblob.set(asf)
    image_stream = BytesIO()
    asf.savefig(image_stream)
    image_stream.seek(0)
    with BytesIO() as input_blob:
        mypictureblob.set(image_stream)

There is code missing, but it works up to this point. This is 100% a input/output issue or I am trying to incorrectly find the temp folder I created.

Here is my JSON function file below:

enter image description here


Solution

  • I figured it out, in case anyone ever comes across this looking for a solution.

        def return_graph(final_difference):
        final_difference = final_difference
        fig = go.Figure()
        fig.add_trace(go.Scatter(y=final_difference['Expected Supply'],x = final_difference.index,hoverinfo='x+y',
                                mode = 'lines',
                                fill='tozeroy'))
    
        fig.update_layout(
            
            title="24 Month Supply and Demand - Daily Peak Hour",
            xaxis_title="Date",
            yaxis_title="MW Surplus",
            legend_title="",
            font=dict(
                family="Courier New, monospace",
                size=15,
                color="green"
            ))
    
        fig.update_layout(
            title={
                'text': "24 Month Supply and Demand - Daily Peak Hour",
                'y':0.9,
                'x':0.5,
                'xanchor': 'center',
                'yanchor': 'top'})
        tempFilePath = tempfile.gettempdir()
        fp = tempfile.NamedTemporaryFile()
        
        fig_div = fig.to_html()
        mypictureblob.set(fig_div)
    

    The difference being fig.to_html vs fig.write_html.