top bantz
top bantz

Reputation: 615

Plotly giving error message "TypeError: __init__() got an unexpected keyword argument 'encoding'" - Python

I only came across Plotly yesterday, I'm trying to find a way to produce tables in a nice way, similar to producing charts in Matplotlib.

Initially I tried to use my own data, and kept getting the error message in the title. So I have copy and pasted the exact code from the plotly website and still get this error. Has anyone encountered this before? Does anyone have a solution. I get the feeling this isn't a simple problem with my code.

import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv')

trace = go.Table(
    header=dict(values=list(df.columns),
                fill = dict(color='#C2D4FF'),
                align = ['left'] * 5),
    cells=dict(values=[df.Rank, df.State, df.Postal, df.Population],
               fill = dict(color='#F5F8FF'),
               align = ['left'] * 5))

data = [trace]
py.iplot(data, filename = 'pandas_table')

This is the code I have used, that results in the following error:

TypeError: __init__() got an unexpected keyword argument 'encoding'

If anyone has an alternative to plotly, where I could produce nice looking tables that would also be really helpful.

Many thanks

Upvotes: 3

Views: 6725

Answers (2)

Andrei Dobre
Andrei Dobre

Reputation: 103

It looks like a bug. If you are in a hurry and can't wait for a fix you can do this:

Open /usr/lib/python3/dist-packages/simplejson/__init__.py and edit the 'cls' variable inside the dumps method as such: cls = JSONEncoder

So it looks like this:

    if cls is None:
        cls = JSONEncoder
    cls = JSONEncoder
    return cls(
        skipkeys=skipkeys, ensure_ascii=ensure_ascii,
        check_circular=check_circular, allow_nan=allow_nan, indent=indent,
        separators=separators, encoding=encoding, default=default,
        use_decimal=use_decimal,
        namedtuple_as_object=namedtuple_as_object,
        tuple_as_array=tuple_as_array,
        iterable_as_array=iterable_as_array,
        bigint_as_string=bigint_as_string,
        sort_keys=sort_keys,
        item_sort_key=item_sort_key,
        for_json=for_json,
        ignore_nan=ignore_nan,
        int_as_string_bitcount=int_as_string_bitcount,
        **kw).encode(obj)

Basically force it to use the default JSONEncoder instead of the Plotly encoder. Also make sure the change is not disruptive for any other code where you use JSON. It worked for me, but there surely are better solutions.

Upvotes: 2

vlizana
vlizana

Reputation: 3232

Depends on your environment. If you're using notebooks, qgrid is a really nice tool for working with pandas dataframes but I don't know if it works in other environments.

Regarding the code, it works for me (again in a notebook environment) by changing

import plotly.plotly as py

for

import plotly.offline as py
py.init_notebook_mode(connected=False)

I tend to use offline plotly, which is standard in plotly 3.

Upvotes: 6

Related Questions