7
7
from typing import Any , Callable , Dict , Optional
8
8
from uuid import uuid4
9
9
10
- import y_py as Y
10
+ from pycrdt import Array , Doc , Map , Text
11
11
12
12
from .utils import cast_all
13
13
from .ybasedoc import YBaseDoc
@@ -47,16 +47,18 @@ class YNotebook(YBaseDoc):
47
47
}
48
48
"""
49
49
50
- def __init__ (self , ydoc : Optional [Y . YDoc ] = None ):
50
+ def __init__ (self , ydoc : Optional [Doc ] = None ):
51
51
"""
52
52
Constructs a YNotebook.
53
53
54
- :param ydoc: The :class:`y_py.YDoc ` that will hold the data of the document, if provided.
55
- :type ydoc: :class:`y_py.YDoc `, optional.
54
+ :param ydoc: The :class:`pycrdt.Doc ` that will hold the data of the document, if provided.
55
+ :type ydoc: :class:`pycrdt.Doc `, optional.
56
56
"""
57
57
super ().__init__ (ydoc )
58
- self ._ymeta = self ._ydoc .get_map ("meta" )
59
- self ._ycells = self ._ydoc .get_array ("cells" )
58
+ self ._ymeta = Map ()
59
+ self ._ycells = Array ()
60
+ self ._ydoc ["meta" ] = self ._ymeta
61
+ self ._ydoc ["cells" ] = self ._ycells
60
62
61
63
@property
62
64
def version (self ) -> str :
@@ -74,7 +76,7 @@ def ycells(self):
74
76
Returns the Y-cells.
75
77
76
78
:return: The Y-cells.
77
- :rtype: :class:`y_py.YArray `
79
+ :rtype: :class:`pycrdt.Array `
78
80
"""
79
81
return self ._ycells
80
82
@@ -98,8 +100,8 @@ def get_cell(self, index: int) -> Dict[str, Any]:
98
100
:return: A cell.
99
101
:rtype: Dict[str, Any]
100
102
"""
101
- meta = json .loads (self ._ymeta . to_json ( ))
102
- cell = json .loads (self ._ycells [index ]. to_json ( ))
103
+ meta = json .loads (str ( self ._ymeta ))
104
+ cell = json .loads (str ( self ._ycells [index ]))
103
105
cast_all (cell , float , int ) # cells coming from Yjs have e.g. execution_count as float
104
106
if "id" in cell and meta ["nbformat" ] == 4 and meta ["nbformat_minor" ] <= 4 :
105
107
# strip cell IDs if we have notebook format 4.0-4.4
@@ -112,26 +114,17 @@ def get_cell(self, index: int) -> Dict[str, Any]:
112
114
del cell ["attachments" ]
113
115
return cell
114
116
115
- def append_cell (self , value : Dict [str , Any ], txn : Optional [ Y . YTransaction ] = None ) -> None :
117
+ def append_cell (self , value : Dict [str , Any ]) -> None :
116
118
"""
117
119
Appends a cell.
118
120
119
121
:param value: A cell.
120
122
:type value: Dict[str, Any]
121
-
122
- :param txn: A YTransaction, defaults to None
123
- :type txn: :class:`y_py.YTransaction`, optional.
124
123
"""
125
124
ycell = self .create_ycell (value )
126
- if txn is None :
127
- with self ._ydoc .begin_transaction () as txn :
128
- self ._ycells .append (txn , ycell )
129
- else :
130
- self ._ycells .append (txn , ycell )
131
-
132
- def set_cell (
133
- self , index : int , value : Dict [str , Any ], txn : Optional [Y .YTransaction ] = None
134
- ) -> None :
125
+ self ._ycells .append (ycell )
126
+
127
+ def set_cell (self , index : int , value : Dict [str , Any ]) -> None :
135
128
"""
136
129
Sets a cell into indicated position.
137
130
@@ -140,60 +133,48 @@ def set_cell(
140
133
141
134
:param value: A cell.
142
135
:type value: Dict[str, Any]
143
-
144
- :param txn: A YTransaction, defaults to None
145
- :type txn: :class:`y_py.YTransaction`, optional.
146
136
"""
147
137
ycell = self .create_ycell (value )
148
- self .set_ycell (index , ycell , txn )
138
+ self .set_ycell (index , ycell )
149
139
150
- def create_ycell (self , value : Dict [str , Any ]) -> Y . YMap :
140
+ def create_ycell (self , value : Dict [str , Any ]) -> Map :
151
141
"""
152
142
Creates YMap with the content of the cell.
153
143
154
144
:param value: A cell.
155
145
:type value: Dict[str, Any]
156
146
157
147
:return: A new cell.
158
- :rtype: :class:`y_py.YMap `
148
+ :rtype: :class:`pycrdt.Map `
159
149
"""
160
150
cell = copy .deepcopy (value )
161
151
if "id" not in cell :
162
152
cell ["id" ] = str (uuid4 ())
163
153
cell_type = cell ["cell_type" ]
164
154
cell_source = cell ["source" ]
165
155
cell_source = "" .join (cell_source ) if isinstance (cell_source , list ) else cell_source
166
- cell ["source" ] = Y . YText (cell_source )
167
- cell ["metadata" ] = Y . YMap (cell .get ("metadata" , {}))
156
+ cell ["source" ] = Text (cell_source )
157
+ cell ["metadata" ] = Map (cell .get ("metadata" , {}))
168
158
169
159
if cell_type in ("raw" , "markdown" ):
170
160
if "attachments" in cell and not cell ["attachments" ]:
171
161
del cell ["attachments" ]
172
162
elif cell_type == "code" :
173
- cell ["outputs" ] = Y . YArray (cell .get ("outputs" , []))
163
+ cell ["outputs" ] = Array (cell .get ("outputs" , []))
174
164
175
- return Y . YMap (cell )
165
+ return Map (cell )
176
166
177
- def set_ycell (self , index : int , ycell : Y . YMap , txn : Optional [ Y . YTransaction ] = None ) -> None :
167
+ def set_ycell (self , index : int , ycell : Map ) -> None :
178
168
"""
179
169
Sets a Y cell into the indicated position.
180
170
181
171
:param index: The index of the cell.
182
172
:type index: int
183
173
184
174
:param ycell: A YMap with the content of a cell.
185
- :type ycell: :class:`y_py.YMap`
186
-
187
- :param txn: A YTransaction, defaults to None
188
- :type txn: :class:`y_py.YTransaction`, optional.
175
+ :type ycell: :class:`pycrdt.Map`
189
176
"""
190
- if txn is None :
191
- with self ._ydoc .begin_transaction () as txn :
192
- self ._ycells .delete (txn , index )
193
- self ._ycells .insert (txn , index , ycell )
194
- else :
195
- self ._ycells .delete (txn , index )
196
- self ._ycells .insert (txn , index , ycell )
177
+ self ._ycells [index ] = ycell
197
178
198
179
def get (self ) -> Dict :
199
180
"""
@@ -202,7 +183,7 @@ def get(self) -> Dict:
202
183
:return: Document's content.
203
184
:rtype: Dict
204
185
"""
205
- meta = json .loads (self ._ymeta . to_json ( ))
186
+ meta = json .loads (str ( self ._ymeta ))
206
187
cast_all (meta , float , int ) # notebook coming from Yjs has e.g. nbformat as float
207
188
cells = []
208
189
for i in range (len (self ._ycells )):
@@ -247,29 +228,23 @@ def set(self, value: Dict) -> None:
247
228
}
248
229
]
249
230
250
- with self ._ydoc .begin_transaction () as t :
231
+ with self ._ydoc .transaction () :
251
232
# clear document
252
- cells_len = len (self ._ycells )
253
- for key in self ._ymeta :
254
- self ._ymeta .pop (t , key )
255
- if cells_len :
256
- self ._ycells .delete_range (t , 0 , cells_len )
257
- for key in [k for k in self ._ystate if k not in ("dirty" , "path" )]:
258
- self ._ystate .pop (t , key )
233
+ self ._ymeta .clear ()
234
+ self ._ycells .clear ()
235
+ for key in [k for k in self ._ystate .keys () if k not in ("dirty" , "path" )]:
236
+ del self ._ystate [key ]
259
237
260
238
# initialize document
261
- # workaround for https://github.com/y-crdt/ypy/issues/126:
262
- # self._ycells.extend(t, [self.create_ycell(cell) for cell in cells])
263
- for cell in cells :
264
- self ._ycells .append (t , self .create_ycell (cell ))
265
- self ._ymeta .set (t , "nbformat" , nb .get ("nbformat" , NBFORMAT_MAJOR_VERSION ))
266
- self ._ymeta .set (t , "nbformat_minor" , nb .get ("nbformat_minor" , NBFORMAT_MINOR_VERSION ))
239
+ self ._ycells .extend ([self .create_ycell (cell ) for cell in cells ])
240
+ self ._ymeta ["nbformat" ] = nb .get ("nbformat" , NBFORMAT_MAJOR_VERSION )
241
+ self ._ymeta ["nbformat_minor" ] = nb .get ("nbformat_minor" , NBFORMAT_MINOR_VERSION )
267
242
268
243
metadata = nb .get ("metadata" , {})
269
244
metadata .setdefault ("language_info" , {"name" : "" })
270
245
metadata .setdefault ("kernelspec" , {"name" : "" , "display_name" : "" })
271
246
272
- self ._ymeta . set ( t , "metadata" , Y . YMap (metadata ) )
247
+ self ._ymeta [ "metadata" ] = Map (metadata )
273
248
274
249
def observe (self , callback : Callable [[str , Any ], None ]) -> None :
275
250
"""
0 commit comments