@@ -99,6 +99,10 @@ def _serialize_plotly_figure(data: typing.Any) -> typing.Optional[tuple[str, str
99
99
return None
100
100
mimetype = "application/vnd.plotly.v1+json"
101
101
data = plotly .io .to_json (data , engine = "json" )
102
+ mfile = BytesIO ()
103
+ mfile .write (data .encode ())
104
+ mfile .seek (0 )
105
+ data = mfile .read ()
102
106
return data , mimetype
103
107
104
108
@@ -110,6 +114,10 @@ def _serialize_matplotlib(data: typing.Any) -> typing.Optional[tuple[str, str]]:
110
114
return None
111
115
mimetype = "application/vnd.plotly.v1+json"
112
116
data = plotly .io .to_json (plotly .tools .mpl_to_plotly (data .gcf ()), engine = "json" )
117
+ mfile = BytesIO ()
118
+ mfile .write (data .encode ())
119
+ mfile .seek (0 )
120
+ data = mfile .read ()
113
121
return data , mimetype
114
122
115
123
@@ -121,6 +129,10 @@ def _serialize_matplotlib_figure(data: typing.Any) -> typing.Optional[tuple[str,
121
129
return None
122
130
mimetype = "application/vnd.plotly.v1+json"
123
131
data = plotly .io .to_json (plotly .tools .mpl_to_plotly (data ), engine = "json" )
132
+ mfile = BytesIO ()
133
+ mfile .write (data .encode ())
134
+ mfile .seek (0 )
135
+ data = mfile .read ()
124
136
return data , mimetype
125
137
126
138
@@ -161,8 +173,11 @@ def _serialize_torch_tensor(data: typing.Any) -> typing.Optional[tuple[str, str]
161
173
def _serialize_json (data : typing .Any ) -> typing .Optional [tuple [str , str ]]:
162
174
mimetype = "application/json"
163
175
try :
164
- data = json .dumps (data )
165
- except TypeError :
176
+ mfile = BytesIO ()
177
+ mfile .write (json .dumps (data ).encode ())
178
+ mfile .seek (0 )
179
+ data = mfile .read ()
180
+ except (TypeError , json .JSONDecodeError ):
166
181
return None
167
182
return data , mimetype
168
183
0 commit comments