Skip to content

Commit

Permalink
[0.5.1] fix show image caton
Browse files Browse the repository at this point in the history
  • Loading branch information
junhuanchen committed Jul 13, 2022
1 parent 3915100 commit 298f2f1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
61 changes: 32 additions & 29 deletions rpyc_ikernel/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import time
import traceback
import urllib.request
import io
import re
import _thread
import socket
Expand Down Expand Up @@ -63,7 +62,7 @@ def read_header_line(self, stream):
headers like Content-Type and Content-Length which determine the type and
length of the data portion.
'''
return stream.readline().decode('utf-8').strip()
return stream.readline().decode('iso8859-1').strip()


def read_headers(self, stream, boundary):
Expand Down Expand Up @@ -424,34 +423,38 @@ def _update_display(self):
if self._media_client != None:
# for content in self._media_client.iter_content():
# self.log.info('iter_content... (%s)' % len(content))
content = next(self._media_client.iter_content())
tmp = Image.open(io.BytesIO(content))
buf = io.BytesIO()
tmp.resize((tmp.size[0], tmp.size[1])).save(buf, format = "JPEG")
buf.flush()
content = buf.getvalue()
if self.clear_output: # used when updating lines printed
self.send_response(self.iopub_socket,
'clear_output', {"wait": True})
# self.log.debug(content)
image_type = imghdr.what(None, content)
# self.log.debug(image_type)
image_data = base64.b64encode(content).decode('ascii')
# self.log.debug(image_data)
self.send_response(self.iopub_socket, 'display_data', {
'data': {
'image/' + image_type: image_data
},
'metadata': {}
})
try:
content = next(self._media_client.iter_content())
image_type = imghdr.what(None, content)
# import io
# self.log.info(image_type)
# img = io.BytesIO(content)
# Image.open(img)
# tmp = Image.open()
# buf = io.BytesIO()
# tmp.resize((tmp.size[0], tmp.size[1])).save(buf, format = "JPEG")
# buf.flush()
# content = buf.getvalue()
if self.clear_output: # used when updating lines printed
self.send_response(self.iopub_socket,
'clear_output', {"wait": True})
# self.log.info(content)
# image_type = imghdr.what(None, content)
# self.log.info(image_type)
image_data = base64.b64encode(content).decode('iso8859-1')
# self.log.info(len(image_data))
self.send_response(self.iopub_socket, 'display_data', {
'data': {
'image/' + image_type: image_data
},
'metadata': {}
})
except Exception as e:
self.log.debug('[%s] Exception %s' % (self._media_client, e))
# except UnicodeDecodeError as e:
# self.log.info('[%s] UnicodeDecodeError ' % (self._media_client))
# # self._media_client.stream.close()
# # self._media_client = None
# except UnidentifiedImageError as e:
# self.log.info('[%s] UnidentifiedImageError ' % (self._media_client))
# # self._media_client.stream.close()
# # self._media_client = None
# self.log.debug('[%s] UnicodeDecodeError %s ' % (self._media_client, e))
except Image.UnidentifiedImageError as e:
self.log.debug('[%s] UnidentifiedImageError %s' % (self._media_client, e))
# time.sleep(0.02)
time.sleep(0.01)
# except OSError as e:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name="rpyc_ikernel",
version="0.4.0",
version="0.5.1",
description="rpyc for jupyter kernel",
long_description=open('README.md', 'r', encoding='UTF-8').read(),
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 298f2f1

Please sign in to comment.