Skip to content

Commit ebcac11

Browse files
author
Matt Kafonek
authored
Add update cell content method to rtu client (#150)
* Add update cell content method to rtu client * add replace cell content method * fix syntax * fix changelog * tests
1 parent 82c9669 commit ebcac11

File tree

3 files changed

+84
-21
lines changed

3 files changed

+84
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
For pre-1.0 releases, see [0.0.35 Changelog](https://github.com/noteable-io/origami/blob/0.0.35/CHANGELOG.md)
88

99
## [Unreleased]
10+
### Added
11+
- `rtu_client.update_cell_content` that takes cell id and a diff-match-patch patch str
1012

1113
### Changed
1214
- Temporary guard against invalid `rtu_client_type` names when initiating `APIClient`

origami/clients/rtu.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from sending.backends.websocket import WebsocketManager
1818
from websockets.client import WebSocketClientProtocol
1919

20+
from origami.models.deltas.delta_types.cell_contents import CellContentsReplace, CellContentsUpdate
2021
from origami.models.deltas.delta_types.cell_execute import (
2122
CellExecute,
2223
CellExecuteAfter,
@@ -822,6 +823,30 @@ async def change_cell_type(
822823
_, cell = self.builder.get_cell(cell_id)
823824
return cell
824825

826+
async def update_cell_content(self, cell_id: str, patch: str) -> NotebookCell:
827+
"""
828+
Update cell content with a diff-match-patch patch string
829+
"""
830+
delta = CellContentsUpdate(
831+
file_id=self.file_id, resource_id=cell_id, properties={'patch': patch}
832+
)
833+
await self.new_delta_request(delta)
834+
# Grab updated cell post-squashing
835+
_, cell = self.builder.get_cell(cell_id)
836+
return cell
837+
838+
async def replace_cell_content(self, cell_id: str, source: str) -> NotebookCell:
839+
"""
840+
Replace cell content with a string
841+
"""
842+
delta = CellContentsReplace(
843+
file_id=self.file_id, resource_id=cell_id, properties={'source': source}
844+
)
845+
await self.new_delta_request(delta)
846+
# Grab updated cell post-squashing
847+
_, cell = self.builder.get_cell(cell_id)
848+
return cell
849+
825850
async def queue_execution(
826851
self,
827852
cell_id: Optional[str] = None,

tests/e2e/rtu/test_notebook.py

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,73 @@ async def test_add_and_remove_cell(api_client: APIClient, notebook_maker):
1313
# TODO: remove sleep when Gate stops permission denied on newly created files (db time-travel)
1414
await asyncio.sleep(2)
1515
rtu_client: RTUClient = await api_client.connect_realtime(file)
16-
assert rtu_client.builder.nb.cells == []
16+
try:
17+
assert rtu_client.builder.nb.cells == []
1718

18-
cell = await rtu_client.add_cell(source='print("hello world")')
19-
assert cell.cell_type == 'code'
20-
assert cell.id in rtu_client.cell_ids
19+
cell = await rtu_client.add_cell(source='print("hello world")')
20+
assert cell.cell_type == 'code'
21+
assert cell.id in rtu_client.cell_ids
2122

22-
await rtu_client.delete_cell(cell.id)
23-
with pytest.raises(CellNotFound):
24-
rtu_client.builder.get_cell(cell.id)
25-
26-
await rtu_client.shutdown()
23+
await rtu_client.delete_cell(cell.id)
24+
with pytest.raises(CellNotFound):
25+
rtu_client.builder.get_cell(cell.id)
26+
finally:
27+
await rtu_client.shutdown()
2728

2829

2930
async def test_change_cell_type(api_client: APIClient, notebook_maker):
3031
file: File = await notebook_maker()
3132
# TODO: remove sleep when Gate stops permission denied on newly created files (db time-travel)
3233
await asyncio.sleep(2)
3334
rtu_client: RTUClient = await api_client.connect_realtime(file)
34-
assert rtu_client.builder.nb.cells == []
35+
try:
36+
assert rtu_client.builder.nb.cells == []
37+
38+
source_cell = await rtu_client.add_cell(source='1 + 1')
39+
_, cell = rtu_client.builder.get_cell(source_cell.id)
40+
assert cell.cell_type == 'code'
41+
42+
await rtu_client.change_cell_type(cell.id, 'markdown')
43+
_, cell = rtu_client.builder.get_cell(source_cell.id)
44+
assert cell.cell_type == 'markdown'
45+
46+
await rtu_client.change_cell_type(cell.id, 'sql')
47+
_, cell = rtu_client.builder.get_cell(source_cell.id)
48+
assert cell.cell_type == 'code'
49+
assert cell.is_sql_cell
50+
finally:
51+
await rtu_client.shutdown()
52+
53+
54+
async def test_update_cell_content(api_client: APIClient, notebook_maker):
55+
file: File = await notebook_maker()
56+
# TODO: remove sleep when Gate stops permission denied on newly created files (db time-travel)
57+
await asyncio.sleep(2)
58+
rtu_client: RTUClient = await api_client.connect_realtime(file)
59+
try:
60+
assert rtu_client.builder.nb.cells == []
61+
62+
source_cell = await rtu_client.add_cell(source='1 + 1')
63+
_, cell = rtu_client.builder.get_cell(source_cell.id)
64+
65+
cell = await rtu_client.update_cell_content(cell.id, '@@ -1,5 +1,5 @@\n-1 + 1\n+2 + 2\n')
66+
assert cell.source == '2 + 2'
67+
finally:
68+
await rtu_client.shutdown()
3569

36-
source_cell = await rtu_client.add_cell(source='1 + 1')
37-
_, cell = rtu_client.builder.get_cell(source_cell.id)
38-
assert cell.cell_type == 'code'
3970

40-
await rtu_client.change_cell_type(cell.id, 'markdown')
41-
_, cell = rtu_client.builder.get_cell(source_cell.id)
42-
assert cell.cell_type == 'markdown'
71+
async def test_replace_cell_content(api_client: APIClient, notebook_maker):
72+
file: File = await notebook_maker()
73+
# TODO: remove sleep when Gate stops permission denied on newly created files (db time-travel)
74+
await asyncio.sleep(2)
75+
rtu_client: RTUClient = await api_client.connect_realtime(file)
76+
try:
77+
assert rtu_client.builder.nb.cells == []
4378

44-
await rtu_client.change_cell_type(cell.id, 'sql')
45-
_, cell = rtu_client.builder.get_cell(source_cell.id)
46-
assert cell.cell_type == 'code'
47-
assert cell.is_sql_cell
79+
source_cell = await rtu_client.add_cell(source='1 + 1')
80+
_, cell = rtu_client.builder.get_cell(source_cell.id)
4881

49-
await rtu_client.shutdown()
82+
cell = await rtu_client.replace_cell_content(cell.id, '2 + 2')
83+
assert cell.source == '2 + 2'
84+
finally:
85+
await rtu_client.shutdown()

0 commit comments

Comments
 (0)