admin管理员组

文章数量:1122846

I have this route for my fastapi app taking care of displaying favicon.ico:

@app.get('/favicon.ico', include_in_schema=False, response_class=FileResponse)
async def favicon():
    """ favicon - do not show up on plotly graphs """
    return FileResponse("static/favicon.png")

Works fine for anything returning HTMLResponse but not the one rendering a plotly.graph_objects

If I remove the above route I see this in the log when rendering: 127.0.0.1:59780 - "GET /favicon.ico HTTP/1.1" 404

But nothing when it is present.

there is no mention of favicon in .html

A summary of the plotly go.Figure:

import plotly.graph_objects as go
:
    fig = go.Figure(
        data = [
            go.Bar(name="Success", x=data['xpos'], y=data['success_count'], marker={'color':'green'}, text=data['success_tests'], textposition='none'),
            go.Bar(name="Failed", x=data['xpos'], y=data['failed_count'], marker={'color':'red'}, text=data['failed_tests'], textposition='none')
        ],
        layout = go.Layout(barmode='stack', xaxis={'type': 'category'})
    )

    for i,px in enumerate(data['xpos']):
        nice = timegraph['nice'][i]
        url = data['urls'][i]
        fig.add_annotation(x=px, xshift=10, y=0, yshift=80, text=f"<a href=\"{url}\" alt=_blank>{px} - {nice}</a>", textangle=270, bgcolor="#FFFFFF", opacity=0.5)

    fig.add_trace(
        go.Scatter(
            x=data['xpos'],
            y=timegraph['ygrid'],
            yaxis="y2",
            name="Total Testrun Time",
            marker={'color': 'gray', 'size': 15},
            text=timegraph['nice'],
            mode='markers',
            zorder=100,
        )
    )
    fig.update_layout(
        plot_bgcolor=plot_bgcolor,
        paper_bgcolor=plot_bgcolor,
        yaxis={
            'title':{'text':"Number of Tests"},
            'side':"left",
            'color': 'green',
            'gridcolor': 'green'
        },
        yaxis2={
            'title':{'text':"Total Testrun Time(s)"},
            'side': "right",
            'overlaying': "y",
            'color': 'gray',
            'showgrid': False,
            'tickformat': '%M:%S'
        },
        title=f"{desc}<br>{sr}</br>(rendertime:{time.time()-starttime:.2f}sec)",
    )
    fig.update_xaxes(showticklabels=False)
    #log.debug(f"fig:{fig}")

    return fig.to_html(config={'displayModeBar': False})

I have this route for my fastapi app taking care of displaying favicon.ico:

@app.get('/favicon.ico', include_in_schema=False, response_class=FileResponse)
async def favicon():
    """ favicon - do not show up on plotly graphs """
    return FileResponse("static/favicon.png")

Works fine for anything returning HTMLResponse but not the one rendering a plotly.graph_objects

If I remove the above route I see this in the log when rendering: 127.0.0.1:59780 - "GET /favicon.ico HTTP/1.1" 404

But nothing when it is present.

there is no mention of favicon in https://plotly.com/python-api-reference/genindex.html

A summary of the plotly go.Figure:

import plotly.graph_objects as go
:
    fig = go.Figure(
        data = [
            go.Bar(name="Success", x=data['xpos'], y=data['success_count'], marker={'color':'green'}, text=data['success_tests'], textposition='none'),
            go.Bar(name="Failed", x=data['xpos'], y=data['failed_count'], marker={'color':'red'}, text=data['failed_tests'], textposition='none')
        ],
        layout = go.Layout(barmode='stack', xaxis={'type': 'category'})
    )

    for i,px in enumerate(data['xpos']):
        nice = timegraph['nice'][i]
        url = data['urls'][i]
        fig.add_annotation(x=px, xshift=10, y=0, yshift=80, text=f"<a href=\"{url}\" alt=_blank>{px} - {nice}</a>", textangle=270, bgcolor="#FFFFFF", opacity=0.5)

    fig.add_trace(
        go.Scatter(
            x=data['xpos'],
            y=timegraph['ygrid'],
            yaxis="y2",
            name="Total Testrun Time",
            marker={'color': 'gray', 'size': 15},
            text=timegraph['nice'],
            mode='markers',
            zorder=100,
        )
    )
    fig.update_layout(
        plot_bgcolor=plot_bgcolor,
        paper_bgcolor=plot_bgcolor,
        yaxis={
            'title':{'text':"Number of Tests"},
            'side':"left",
            'color': 'green',
            'gridcolor': 'green'
        },
        yaxis2={
            'title':{'text':"Total Testrun Time(s)"},
            'side': "right",
            'overlaying': "y",
            'color': 'gray',
            'showgrid': False,
            'tickformat': '%M:%S'
        },
        title=f"{desc}<br>{sr}</br>(rendertime:{time.time()-starttime:.2f}sec)",
    )
    fig.update_xaxes(showticklabels=False)
    #log.debug(f"fig:{fig}")

    return fig.to_html(config={'displayModeBar': False})
Share Improve this question asked yesterday MortenBMortenB 3,4893 gold badges44 silver badges49 bronze badges 1
  • 2 It's not clear what you mean by "I see this in the log when rendering [...]". But you could return only the figure <div> (instead of the whole html document) using fig.to_html(full_html=False), and wrap that div in a custom html page template where you can set your favicon etc. – EricLavault Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 1

Thanks to @EricLavault I changed the return to:

return fig.to_html(config={'displayModeBar': False}, full_html=False)

It shows the favicon.ico, but there is no <html><header></header><body></body></html>structure.

You can render it via file as described here: https://plotly.com/python/interactive-html-export/

本文标签: python plotly add favicon to FigureStack Overflow