Skip to content

Commit 14fa7ae

Browse files
authored
switch to connect_realtime accepting a file or file ID (#133)
1 parent 8505a15 commit 14fa7ae

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Python 3.8+
4444
## Installation
4545

4646
For stable release:
47+
4748
```bash
4849
pip install noteable-origami
4950
```
@@ -53,6 +54,7 @@ poetry add noteable-origami
5354
```
5455

5556
For alpha pre-release:
57+
5658
```bash
5759
pip install noteable-origami --pre
5860
```
@@ -64,8 +66,7 @@ pip install noteable-origami --pre
6466
## Getting Started
6567

6668
> **Warning**
67-
Developer note: this documentation is written for the 1.0 alpha release. For stable release, see [pre-1.0 README](https://github.com/noteable-io/origami/blob/release/0.0.35/README.md)
68-
69+
> Developer note: this documentation is written for the 1.0 alpha release. For stable release, see [pre-1.0 README](https://github.com/noteable-io/origami/blob/release/0.0.35/README.md)
6970
7071
### API Tokens
7172

@@ -76,7 +77,7 @@ The Noteable API requires an authentication token. You can manage tokens at the
7677

7778
### Usage
7879

79-
The example below shows how to create a Notebook, launch a Kernel, add new cells, and execute code.
80+
The example below shows how to create a Notebook, launch a Kernel, add new cells, and execute code.
8081

8182
```python
8283
# Grab a project_id from the Noteable UI, the url will look like: app.noteable.io/p/....
@@ -99,15 +100,15 @@ file = await api_client.create_notebook(project_id=project_id, path="Demo.ipynb"
99100
await api_client.launch_kernel(file.id)
100101

101102
# Client for Real-time Updates (RTU), used with Notebooks
102-
rtu_client = await api_client.rtu_client(file.id)
103+
realtime_notebook = await api_client.connect_realtime(file)
103104

104105
# Add a new cell
105106
from origami.models.notebook import CodeCell
106107
cell = CodeCell(source="print('Hello World')")
107-
await rtu_client.add_cell(cell)
108+
await realtime_notebook.add_cell(cell)
108109

109110
# Execute the cell
110-
queued_execution = await rtu_client.queue_execution(cell.id)
111+
queued_execution = await realtime_notebook.queue_execution(cell.id)
111112

112113
# Wait for the execution to be complete, cell is an updated instance of CodeCell with metadata/outputs
113114
cell = await queued_execution
@@ -117,10 +118,8 @@ output_collection = await api_client.get_output_collection(cell.output_collectio
117118
print(output_collection.outputs[0].content.raw) # 'Hello World\n'
118119
```
119120

120-
121121
<!-- --8<-- [end:start] -->
122122

123-
124123
## 1.0 Roadmap
125124

126125
Origami is heading towards a 1.0 release. The alpha release candidate is on Pypi now, installable with a `--pre` flag. The 1.0 release represents a major refactor of the Origami using the best practices and lessons learned from creating multiple production API and RTU clients, including our ChatGPT plugin. It will likely come out of alpha once all of our internal applications are using the Origami 1.0 syntax.
@@ -130,6 +129,7 @@ Origami is heading towards a 1.0 release. The alpha release candidate is on Pypi
130129
See [CONTRIBUTING.md](./CONTRIBUTING.md).
131130

132131
---
132+
133133
<p align="center">Open sourced with ❤️ by <a href="https://noteable.io">Noteable</a> for the community.</p>
134134

135-
<img href="https://pages.noteable.io/private-beta-access" src="https://assets.noteable.io/github/2022-07-29/noteable.png" alt="Boost Data Collaboration with Notebooks">
135+
<img href="https://pages.noteable.io/private-beta-access" src="https://assets.noteable.io/github/2022-07-29/noteable.png" alt="Boost Data Collaboration with Notebooks">

origami/clients/api.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import os
33
import uuid
4-
from typing import List, Literal, Optional
4+
from typing import List, Literal, Optional, Union
55

66
import httpx
77
import pydantic
@@ -284,14 +284,27 @@ async def get_output_collection(
284284
resp.raise_for_status()
285285
return KernelOutputCollection.parse_obj(resp.json())
286286

287-
async def rtu_client(self, file_id: uuid.UUID) -> RTUClient:
287+
async def connect_realtime(self, file: Union[File, uuid.UUID, str]) -> RTUClient:
288288
"""
289289
Create an RTUClient for a Notebook by file id. This will perform the following steps:
290290
- Check /v1/files to get the current version information and presigned download url
291291
- Download seed notebook and create a NotebookBuilder from it
292292
- Create an RTUClient, initialize the websocket connection, authenticate, and subscribe
293293
- Apply delts to in-memory NotebookBuilder
294294
"""
295+
file_id = None
296+
297+
if isinstance(file, str):
298+
file_id = uuid.UUID(file)
299+
elif isinstance(file, uuid.UUID):
300+
file_id = file
301+
elif isinstance(file, File):
302+
file_id = file.id
303+
else:
304+
raise ValueError(f"Must provide a `file_id` or a File, not {file}")
305+
306+
self.add_tags_and_contextvars(file_id=str(file_id))
307+
295308
logger.info(f"Creating RTUClient for file {file_id}")
296309
file = await self.get_file(file_id)
297310
if file.type != 'notebook':

tests/rtu/test_execution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async def test_single_cell(api_client: APIClient, notebook_maker):
1414
# TODO: remove sleep when Gate stops permission denied on newly created files (db time-travel)
1515
await asyncio.sleep(2)
1616

17-
rtu_client: RTUClient = await api_client.rtu_client(file.id)
17+
rtu_client: RTUClient = await api_client.connect_realtime(file)
1818
assert rtu_client.builder.nb.cells == notebook.cells
1919

2020
kernel_session: KernelSession = await api_client.launch_kernel(file.id)
@@ -47,7 +47,7 @@ async def test_run_all(api_client: APIClient, notebook_maker):
4747
# TODO: remove sleep when Gate stops permission denied on newly created files (db time-travel)
4848
await asyncio.sleep(2)
4949

50-
rtu_client: RTUClient = await api_client.rtu_client(file.id)
50+
rtu_client: RTUClient = await api_client.connect_realtime(file)
5151
assert rtu_client.builder.nb.cells == notebook.cells
5252

5353
kernel_session: KernelSession = await api_client.launch_kernel(file.id)

tests/rtu/test_notebook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async def test_add_and_remove_cell(api_client: APIClient, notebook_maker):
1313
file: File = await notebook_maker()
1414
# TODO: remove sleep when Gate stops permission denied on newly created files (db time-travel)
1515
await asyncio.sleep(2)
16-
rtu_client: RTUClient = await api_client.rtu_client(file.id)
16+
rtu_client: RTUClient = await api_client.connect_realtime(file)
1717
assert rtu_client.builder.nb.cells == []
1818

1919
cell = CodeCell(id='cell_1', source='print("hello world")')

0 commit comments

Comments
 (0)