Skip to content

Commit d28c90b

Browse files
authored
Merge pull request #208 from martinRenou/make_orjson_optional
Make orjson optional
2 parents b657d5a + 5d2895e commit d28c90b

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ It allows you to draw simple primitives directly from Python like text, lines, p
1414

1515
You can try it online by clicking on this badge:
1616

17-
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/martinRenou/ipycanvas/stable?urlpath=lab%2Ftree%2Fexamples)
17+
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/martinRenou/ipycanvas/stable?urlpath=lab%2Ftree%2Fexamples)
1818

1919
## Documentation
2020

@@ -32,7 +32,7 @@ Or join the gitter channel: [![Join the chat at https://gitter.im/martinRenou/ip
3232
You can install using `pip`:
3333

3434
```bash
35-
pip install ipycanvas
35+
pip install ipycanvas orjson
3636
```
3737

3838
Or using `conda`:

ipycanvas/utils.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55

66
import numpy as np
77

8-
import orjson
8+
try:
9+
import orjson
10+
ORJSON_AVAILABLE = True
11+
except ImportError:
12+
import json
13+
ORJSON_AVAILABLE = False
914

1015

1116
def image_bytes_to_array(im_bytes):
@@ -71,7 +76,12 @@ def populate_args(arg, args, buffers):
7176

7277
def commands_to_buffer(commands):
7378
# Turn the commands list into a binary buffer
74-
return array_to_binary(np.frombuffer(
75-
bytes(orjson.dumps(commands, option=orjson.OPT_SERIALIZE_NUMPY)),
76-
dtype=np.uint8)
77-
)
79+
if ORJSON_AVAILABLE:
80+
return array_to_binary(np.frombuffer(
81+
bytes(orjson.dumps(commands, option=orjson.OPT_SERIALIZE_NUMPY)),
82+
dtype=np.uint8)
83+
)
84+
else:
85+
return array_to_binary(np.frombuffer(
86+
bytes(json.dumps(commands), encoding='utf8'), dtype=np.uint8
87+
))

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@
9696
install_requires = [
9797
'ipywidgets>=7.6.0',
9898
'pillow>=6.0',
99-
'numpy',
100-
'orjson'
99+
'numpy'
101100
],
102101
extras_require = {},
103102
entry_points = {

0 commit comments

Comments
 (0)