plotly: TypeError: cannot convert dictionary update sequence element #0 to a sequence

I have a problem with the project I'm currently working on, so I'm leaving a question.
The current contents are drawn in table format by sensor. But a few days ago, when I was working on something, I didn't know how to fix it, so I asked for help.

import plotly.plotly as py
import plotly.figure_factory as ff
from bluepy import sensortag
import sys
import time
import datetime
import numpy as np

py.sign_in("smrlswja9963","94b4enXKYqyOu4iuPDGG")

time.sleep(1.0)

tag = sensortag.SensorTag('24:71:89:CC:53:00')
tag.IRtemperature.enable()
tag.humidity.enable()

stream1=py.Stream('liz9v2c0or')
stream1.open()

i=0     
while True:
    now = datetime.datetime.now()
    dateTime = now.strftime("%Y-%m-%d %H:%M:%S")

    A = tag.IRtemperature.read()
    B = tag.humidity.read()
    tempt=A[0]
    humty=B[0]

    stream1.write({dateTime,tempt,humty})

    i+=1

    tag.waitForNotifications(3.0)     

table = go.Table(
    header=dict(values=["dateTime","tempt","humty"]),
    cells=dict(values=[[dateTime],[tempt],[humty]]),
    stream = dict(token='liz9v2c0or',))

data=[table]
py.iplot(table, filename="latex table")

After executing the following code, the following error occurred.

Traceback (most recent call last):
  File "/home/pi/do it.py", line 27, in <module>
    stream1.write({dateTime,tempt,humty})
  File "/usr/local/lib/python2.7/dist-packages/plotly/plotly/plotly.py", line 632, in write
    stream_object.update(trace)
TypeError: cannot convert dictionary update sequence element #0 to a sequence

In this case, the code that I draw the graph using plolty does not have a problem when receiving data from stream, but when I draw table plot.

How can I graph here? I'm really curious. Please let me know.


Solution 1:

This exception means that you're trying to construct a dict from an iterable, and that iterable's first element is not a sequence. As the docs explain, you can construct a dict two ways:

  • From a mapping, or
  • From an iterable of key-value pairs

So, if you try to construct it from, say, a set of numbers:

>>> dict({1, 2, 3})
TypeError: cannot convert dictionary update sequence element #0 to a sequence

… it's trying to use the first element as a key-value pair—that is, a sequence of 2 values—but there's no way to interpret the number 1 as a key-value pair, so it raises a TypeError.


Meanwhile, I know absolutely nothing about Plotly streaming but what's on this page, but this code is clearly wrong:

stream1.write({dateTime,tempt,humty})

I can't imagine why you'd want to stream a set.

Plus, the examples all have either a dict, or a string that's a JSON-encoding of a dict.

So, obviously, that API is expecting you to pass it either a dict or something you can feed to the dict constructor. But you're passing it a set. So, it feeds that set to the dict constructor, and gets this exception.

Since I have no idea what you're actually trying to do here, I have no idea what dict you should be sending here. But you definitely should be passing a dict.


Also, even if you fix this, based on the sign_in call, it looks like you're using Plotly Cloud. But, as the same page says:

Streaming is no longer supported in Plotly Cloud.

So, if you're trying to use streaming with Plotly Cloud, then, even if you fix your code to make sense, it's probably still going to fail, just with an error from Plotly rather than a TypeError about passing nonsense.