Search code examples
pythonplotlysankey-diagram

Plotly AttributeError: 'dict' object has no attribute 'write_html'


I'm attempting to export a Plotly graph as HTML, and the various documentation and SO threads out there suggest methods that fail on the figure object (a dict). Any help appreciated. The final lines are the ones causing the error. The diagram plots fine in an interactive Python window, but I haven't been able to save to html.

Code:

import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)

nodes = [
     ['ID', 'Label', 'Color'],
[0, '1',    '#0313fc'],
[1, '2',    '#109609'],
[2, '3',    '#b9bbf0'],
[3, '4',    '#ff0d00'],
[4, '5',    '#0c0b61'],
[5, '6',    '#cc2e0e'],
[6, '7',    '#000000'],
[7, '8',    '#4b8fe3'],
[8, '9',    '#ff0000'],
[9, '10',   '#1403a8'],
[10,    '11',   '#ff0000'],
[11,    '12',   '#ED1C24'],
[12,    '13',   '#ff0000'],
[13,    '14',   '#bf1f0d'],
[14,    '15',   '#147808'],
[15,    '16',   '#00173d'],
[16,    '17',   '#2a782f'],
[17,    '18',   '#00ad0c'],
[18,    '19',   '#FCD116'],
[19,    '20',   '#00d0ff'],
[20,    '21',   '#ffae00'],
[21,    '22',   '#150191'],
[22,    '23',   '#2200ff'],
[23,    '24',   '#2200ff'],
[24,    '25',   '#034EA2'],
[25,    '26',   '#0008ff'],
[26,    '27',   '#000000'],
[27,    '28',   '#125e05'],
]

links = [
['sum','source','target','link_color'],
[70,11,24,'rgba(237, 28, 36, 0.3)'],
[1341,24,13,'rgba(44, 24, 222, 0.3)'],
[416,18,22,'rgba(252, 209, 22, 0.3)'],
[4369,18,19,'rgba(252, 209, 22, 0.3)'],
[953,1,2,'rgba(16, 150, 9, 0.3)'],
[16170,9,26,'rgba(20, 3, 168, 0.3)'],
[503,18,3,'rgba(252, 209, 22, 0.3)'],
[11000,9,3,'rgba(20, 3, 168, 0.3)'],
[7200,15,3,'rgba(0, 23, 61, 0.3)'],
[862,11,6,'rgba(237, 28, 36, 0.3)'],
[4463,11,8,'rgba(237, 28, 36, 0.3)'],
[1508,19,10,'rgba(0, 208, 255, 0.3)'],
[1470,1,10,'rgba(16, 150, 9, 0.3)'],
[1016,2,10,'rgba(185, 187, 240, 0.3)'],
[1100,11,10,'rgba(237, 28, 36, 0.3)'],
[2382,18,10,'rgba(252, 209, 22, 0.3)'],
[2065,9,10,'rgba(20, 3, 168, 0.3)'],
[450,12,10,'rgba(255, 0, 0, 0.3)'],
[187,15,10,'rgba(0, 23, 61, 0.3)'],
[172,19,17  ,'rgba(0, 208, 255, 0.3)'],
[3246,18,13,'rgba(252, 209, 22, 0.3)'],
[46,1,25,'rgba(16, 150, 9, 0.3)'],
[1108,1,0,'rgba(16, 150, 9, 0.3)'],
[1826,11,0,'rgba(237, 28, 36, 0.3)'],
[5500,19,20,'rgba(0, 208, 255, 0.3)'],
[220,1,20,'rgba(16, 150, 9, 0.3)'],
[10136,11,20,'rgba(237, 28, 36, 0.3)'],
[1070,1,16,'rgba(16, 150, 9, 0.3)'],
[3000,11,16,'rgba(237, 28, 36, 0.3)'],
[1534,18,4,'rgba(252, 209, 22, 0.3)'],
[238,11,7,'rgba(237, 28, 36, 0.3)'],
[589,18,7,'rgba(252, 209, 22, 0.3)'],
[931,11,18,'rgba(237, 28, 36, 0.3)'],
[1539,12,18,'rgba(255, 0, 0, 0.3)'],
[9,14,12,'rgba(255, 0, 0, 0.3)'],
[1818,27,15,'rgba(6, 51, 3, 0.3)'],
[1500,19,15,'rgba(0, 208, 255, 0.3)'],
[171,11,15,'rgba(237, 28, 36, 0.3)'],
[4800,18,15,'rgba(252, 209, 22, 0.3)'],
[199,23,15,'rgba(20, 3, 168, 0.3)'],
[797,12,15,'rgba(255, 0, 0, 0.3)'],
[508,4,21,'rgba(122, 122, 122, 0.3)']
]

nodes_headers = nodes.pop(0)
nodes_df = pd.DataFrame(nodes, columns = nodes_headers)
links_headers = links.pop(0)
links_df = pd.DataFrame(links, columns = links_headers)

data_trace = dict(
    type='sankey',
    domain = dict(
      x =  [0,1],
      y =  [0,1]
    ),
    orientation = "h",
    valueformat = ".0f",
    node = dict(
      pad = 10,
      thickness = 30,
      line = dict(
        color = "black",
        width = 0
      ),
      label =  nodes_df['Label'],
      color = nodes_df['Color']
    ),
    link = dict(
      source = links_df['source'],
      target = links_df['target'],
      value = links_df['sum'],
      color = links_df['link_color'],
  )
)

layout =  dict(
    title = "foobar",
    height = 772,
    width = 1000,
    font = dict(
      size = 12
    ),    
)

fig = dict(data=[data_trace], layout=layout)
# this plots the figure, but doesn't create a file
iplot(fig, filename='sankey.html',validate=False)

# this returns an error
fig.write_html("sankey.html")

Error:

AttributeError: 'dict' object has no attribute 'write_html'


Solution

  • Just change:

    fig = dict(data=[data_trace], layout=layout)
    

    to:

    fig = go.Figure(dict(data=[data_trace], layout=layout))
    

    and you should be good to go.

    On my end the following is produced in the working directory:

    enter image description here