Python Forum
Error message in Jupyter Notebook with json
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error message in Jupyter Notebook with json
#1
Hi,

I am new to Python and Jupyter Notebook and I get the error below in Jupyter Notebook while the code works in Spyder:

import base64
import json
import subprocess

def output_image(name, format, bytes):
    image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8"
    image_end = "END_IMAGE_0238jfw08fjsiufhw8frs"
    data = {}
    data['name'] = name
    data['format'] = format
    data['bytes'] = base64.encodestring(bytes)
    print(image_start + json.dumps(data) + image_end)
Error:
TypeError Traceback (most recent call last) <ipython-input-16-f9e8ef7a162f> in <module>() 1 ### draw the decision boundary with the text points overlaid 2 instanceCV.prettyPicture(clf, features_test, labels_test) ----> 3 instanceCV.output_image("test.png", "png", open("test.png", "rb").read()) <ipython-input-10-99476019b17b> in output_image(self, name, format, bytes) 71 data['format'] = format 72 data['bytes'] = base64.encodestring(bytes) ---> 73 print(image_start + json.dumps(data) + image_end) C:\ProgramData\Anaconda3\lib\json\__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw) 229 cls is None and indent is None and separators is None and 230 default is None and not sort_keys and not kw): --> 231 return _default_encoder.encode(obj) 232 if cls is None: 233 cls = JSONEncoder C:\ProgramData\Anaconda3\lib\json\encoder.py in encode(self, o) 197 # exceptions aren't as detailed. The list call should be roughly 198 # equivalent to the PySequence_Fast that ''.join() would do. --> 199 chunks = self.iterencode(o, _one_shot=True) 200 if not isinstance(chunks, (list, tuple)): 201 chunks = list(chunks) C:\ProgramData\Anaconda3\lib\json\encoder.py in iterencode(self, o, _one_shot) 255 self.key_separator, self.item_separator, self.sort_keys, 256 self.skipkeys, _one_shot) --> 257 return _iterencode(o, 0) 258 259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
Thanks for help
Reply
#2
(Jun-17-2018, 09:37 AM)diet Wrote: Hi,

I am new to Python and Jupyter Notebook and I get the error below in Jupyter Notebook while the code works in Spyder:

import base64
import json
import subprocess

def output_image(name, format, bytes):
    image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8"
    image_end = "END_IMAGE_0238jfw08fjsiufhw8frs"
    data = {}
    data['name'] = name
    data['format'] = format
    data['bytes'] = base64.encodestring(bytes)
    print(image_start + json.dumps(data) + image_end)
One thing that is dangerous in your code - you override (shadow) global name format. Maybe that is not your issue - but it is a potentially dangerous substitution
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
The problem is that you have bytes b'something' in dictionary data.
JSON format only supports Unicode strings.
As mention bye @volcano63 you overwrite format,but also bytes is a used name bye Python.
>>> format
<built-in function format>
>>> bytes
<class 'bytes'>

>>> # A name that not used give NameError
>>> foo
Traceback (most recent call last):
  File "<string>", line 428, in runcode
  File "<interactive input>", line 1, in <module>
NameError: name 'foo' is not defined 
You should also show what you call function with,now do i see that in error message that it's png image.
Since Base64 encodes bytes to ASCII-only bytes,you can use that codec to decode the data so it can be dumped to JSON.
import base64
import json
import subprocess
 
def output_image(name, file_format, img_bytes):
    image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8"
    image_end = "END_IMAGE_0238jfw08fjsiufhw8frs"
    data = {}
    data['name'] = name
    data['format'] = file_format
    encoded = data['bytes'] = base64.encodestring(img_bytes)
    # Fix
    data['bytes'] = encoded.decode('ascii')     
    print(image_start + json.dumps(data) + image_end)
    
output_image('logo.png', 'png', open("logo.png", "rb").read())
Reply
#4
The question remains about working in Spyder and not working in Jupyter. I have a nagging suspicion that Jupyter is running Python3, and Spyder - Python2.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
(Jun-17-2018, 03:13 PM)volcano63 Wrote: I have a nagging suspicion that Jupyter is running Python3, and Spyder - Python2
Yes i think that to.
@diet check from where you run Spyder,for Python 3 Spyder should be run from your Anaconda3\Scripts folder or from Navigator Anaconda3.

Do you have a newer download of Anaconda3 Spyder may not be there bye default(may need install),
as you see in VS Code Bundled with Anaconda so have they switch to VS Code and do not support Spyder anymore.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  json loads throwing error mpsameer 8 711 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 1,052 Nov-15-2023, 06:56 PM
Last Post: jst
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 720 Oct-29-2023, 12:40 PM
Last Post: Mark17
  Error message about iid from RandomizedSearchCV Visiting 2 1,037 Aug-17-2023, 07:53 PM
Last Post: Visiting
  json decoding error deneme2 10 3,681 Mar-22-2023, 10:44 PM
Last Post: deanhystad
  Another Error message. the_jl_zone 2 991 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  How to programmatically stop a program in Jupyter Notebook? Mark17 11 37,254 Feb-12-2023, 01:41 PM
Last Post: jp21in
Thumbs Up Python 3 Jupyter notebook ternary plot data nicholas 0 955 Jan-21-2023, 05:01 PM
Last Post: nicholas
  python requests library .JSON() error mHosseinDS86 6 3,453 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  Graphs from Jupyter notebook to word Scott 0 892 Nov-28-2022, 06:16 AM
Last Post: Scott

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020