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,16 @@ 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 ._ydoc [ "meta" ] = self ._ymeta = Map ( )
59
+ self ._ydoc [ "cells" ] = self ._ycells = Array ( )
60
60
61
61
@property
62
62
def version (self ) -> str :
@@ -74,7 +74,7 @@ def ycells(self):
74
74
Returns the Y-cells.
75
75
76
76
:return: The Y-cells.
77
- :rtype: :class:`y_py.YArray `
77
+ :rtype: :class:`pycrdt.Array `
78
78
"""
79
79
return self ._ycells
80
80
@@ -98,8 +98,8 @@ def get_cell(self, index: int) -> Dict[str, Any]:
98
98
:return: A cell.
99
99
:rtype: Dict[str, Any]
100
100
"""
101
- meta = json .loads (self ._ymeta . to_json ( ))
102
- cell = json .loads (self ._ycells [index ]. to_json ( ))
101
+ meta = json .loads (str ( self ._ymeta ))
102
+ cell = json .loads (str ( self ._ycells [index ]))
103
103
cast_all (cell , float , int ) # cells coming from Yjs have e.g. execution_count as float
104
104
if "id" in cell and meta ["nbformat" ] == 4 and meta ["nbformat_minor" ] <= 4 :
105
105
# strip cell IDs if we have notebook format 4.0-4.4
@@ -112,26 +112,17 @@ def get_cell(self, index: int) -> Dict[str, Any]:
112
112
del cell ["attachments" ]
113
113
return cell
114
114
115
- def append_cell (self , value : Dict [str , Any ], txn : Optional [ Y . YTransaction ] = None ) -> None :
115
+ def append_cell (self , value : Dict [str , Any ]) -> None :
116
116
"""
117
117
Appends a cell.
118
118
119
119
:param value: A cell.
120
120
:type value: Dict[str, Any]
121
-
122
- :param txn: A YTransaction, defaults to None
123
- :type txn: :class:`y_py.YTransaction`, optional.
124
121
"""
125
122
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 :
123
+ self ._ycells .append (ycell )
124
+
125
+ def set_cell (self , index : int , value : Dict [str , Any ]) -> None :
135
126
"""
136
127
Sets a cell into indicated position.
137
128
@@ -140,60 +131,48 @@ def set_cell(
140
131
141
132
:param value: A cell.
142
133
:type value: Dict[str, Any]
143
-
144
- :param txn: A YTransaction, defaults to None
145
- :type txn: :class:`y_py.YTransaction`, optional.
146
134
"""
147
135
ycell = self .create_ycell (value )
148
- self .set_ycell (index , ycell , txn )
136
+ self .set_ycell (index , ycell )
149
137
150
- def create_ycell (self , value : Dict [str , Any ]) -> Y . YMap :
138
+ def create_ycell (self , value : Dict [str , Any ]) -> Map :
151
139
"""
152
140
Creates YMap with the content of the cell.
153
141
154
142
:param value: A cell.
155
143
:type value: Dict[str, Any]
156
144
157
145
:return: A new cell.
158
- :rtype: :class:`y_py.YMap `
146
+ :rtype: :class:`pycrdt.Map `
159
147
"""
160
148
cell = copy .deepcopy (value )
161
149
if "id" not in cell :
162
150
cell ["id" ] = str (uuid4 ())
163
151
cell_type = cell ["cell_type" ]
164
152
cell_source = cell ["source" ]
165
153
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" , {}))
154
+ cell ["source" ] = Text (cell_source )
155
+ cell ["metadata" ] = Map (cell .get ("metadata" , {}))
168
156
169
157
if cell_type in ("raw" , "markdown" ):
170
158
if "attachments" in cell and not cell ["attachments" ]:
171
159
del cell ["attachments" ]
172
160
elif cell_type == "code" :
173
- cell ["outputs" ] = Y . YArray (cell .get ("outputs" , []))
161
+ cell ["outputs" ] = Array (cell .get ("outputs" , []))
174
162
175
- return Y . YMap (cell )
163
+ return Map (cell )
176
164
177
- def set_ycell (self , index : int , ycell : Y . YMap , txn : Optional [ Y . YTransaction ] = None ) -> None :
165
+ def set_ycell (self , index : int , ycell : Map ) -> None :
178
166
"""
179
167
Sets a Y cell into the indicated position.
180
168
181
169
:param index: The index of the cell.
182
170
:type index: int
183
171
184
172
: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.
173
+ :type ycell: :class:`pycrdt.Map`
189
174
"""
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 )
175
+ self ._ycells [index ] = ycell
197
176
198
177
def get (self ) -> Dict :
199
178
"""
@@ -202,7 +181,7 @@ def get(self) -> Dict:
202
181
:return: Document's content.
203
182
:rtype: Dict
204
183
"""
205
- meta = json .loads (self ._ymeta . to_json ( ))
184
+ meta = json .loads (str ( self ._ymeta ))
206
185
cast_all (meta , float , int ) # notebook coming from Yjs has e.g. nbformat as float
207
186
cells = []
208
187
for i in range (len (self ._ycells )):
@@ -247,29 +226,23 @@ def set(self, value: Dict) -> None:
247
226
}
248
227
]
249
228
250
- with self ._ydoc .begin_transaction () as t :
229
+ with self ._ydoc .transaction () :
251
230
# 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 )
231
+ self ._ymeta .clear ()
232
+ self ._ycells .clear ()
233
+ for key in [k for k in self ._ystate .keys () if k not in ("dirty" , "path" )]:
234
+ del self ._ystate [key ]
259
235
260
236
# 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 ))
237
+ self ._ycells .extend ([self .create_ycell (cell ) for cell in cells ])
238
+ self ._ymeta ["nbformat" ] = nb .get ("nbformat" , NBFORMAT_MAJOR_VERSION )
239
+ self ._ymeta ["nbformat_minor" ] = nb .get ("nbformat_minor" , NBFORMAT_MINOR_VERSION )
267
240
268
241
metadata = nb .get ("metadata" , {})
269
242
metadata .setdefault ("language_info" , {"name" : "" })
270
243
metadata .setdefault ("kernelspec" , {"name" : "" , "display_name" : "" })
271
244
272
- self ._ymeta . set ( t , "metadata" , Y . YMap (metadata ) )
245
+ self ._ymeta [ "metadata" ] = Map (metadata )
273
246
274
247
def observe (self , callback : Callable [[str , Any ], None ]) -> None :
275
248
"""
0 commit comments