Search code examples
pythonplotlyplotly-python

How to hide produced with plotly in Python Flask


I am building a simple analytics application with Flask. I need to hide the produced with plotly. And from the docs. The only option I see to hide is with the fig.show(config={"displaylogo": False}) unfortunately this will not work inside the app as it will open as a separate tab rather than inside the application.

Below is my plot to render the plotly figure as a JSON

def plot_channel_contribution(path):
    data = pd.read_excel(
        path,
        sheet_name="Pie Chart Contribution",
        index_col=0,
        encoding="latin-1",
    )
    data = data.reset_index()
    fig = px.pie(
        data, values="Total Contribution", names="Channel", title="Channel Contribution"
    )

    fig.update_layout(
        title_x=0.5,
        autosize=False,
        width=1000,
        height=600,
    )
    return json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)

In JS, I have tried:

<script type="text/javascript">
      var graph = {{plotly_fig| safe}};
      Plotly.plot('plotly-figure-id',graph, {}, {displaylogo: false});
</script>

But nothing works, any tips or suggestions please?


Solution

  • I found the solution to be in JS:

    var graph = {{plotly_fig| safe}};
    
    graph.config={displayModeBar: false}
    
    Plotly.newPlot('plotly-figure-id',graphs,{});