Skip to content

Commit 9e2d348

Browse files
feat: Add client for Notebook & Notebook Execution APIs (#105)
Co-authored-by: rmilea <[email protected]>
1 parent ef3a013 commit 9e2d348

20 files changed

+1867
-3
lines changed

docs/api_reference.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ API Reference
1414
api_reference/dataframe
1515
api_reference/spec
1616
api_reference/file
17+
api_reference/notebook
1718
api_reference/feeds
1819

1920
Indices and tables

docs/api_reference/notebook.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.. _api_tag_page:
2+
3+
nisystemlink.clients.notebook
4+
======================
5+
6+
.. autoclass:: nisystemlink.clients.notebook.NotebookClient
7+
:exclude-members: __init__
8+
9+
.. automethod:: __init__
10+
.. automethod:: get_notebook
11+
.. automethod:: update_notebook
12+
.. automethod:: delete_notebook
13+
.. automethod:: create_notebook
14+
.. automethod:: query_notebooks
15+
.. automethod:: get_notebook_content
16+
.. automethod:: create_executions
17+
.. automethod:: get_execution_by_id
18+
.. automethod:: query_executions
19+
20+
21+
.. automodule:: nisystemlink.clients.notebook.models
22+
:members:
23+
:imported-members:

docs/getting_started.rst

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ Create, query, update, and delete some products
106106

107107
DataFrame API
108108
-------
109-
110109
Overview
111110
~~~~~~~~
112111

@@ -186,6 +185,7 @@ Update and Delete Specifications
186185
:language: python
187186
:linenos:
188187

188+
189189
File API
190190
-------
191191

@@ -285,5 +285,38 @@ Create, query, update, and delete some results
285285
Create, update, query, and delete steps
286286

287287
.. literalinclude:: ../examples/testmonitor/steps.py
288+
:language: python
289+
:linenos
290+
291+
Notebook API
292+
-------
293+
294+
Overview
295+
~~~~~~~~
296+
297+
The :class:`.NotebookClient` class is the primary entry point of the Notebook API.
298+
299+
When constructing a :class:`.NotebookClient`, you can pass an
300+
:class:`.HttpConfiguration` (like one retrieved from the
301+
:class:`.HttpConfigurationManager`), or let :class:`.NotebookClient` use the
302+
default connection. The default connection depends on your environment.
303+
304+
With a :class:`.NotebookClient` object, you can:
305+
306+
* Create, update, query, and delete Notebooks
307+
* Create, get and query Notebook Executions
308+
309+
Examples
310+
~~~~~~~~
311+
312+
Create, query, update, and delete some notebooks.
313+
314+
.. literalinclude:: ../examples/notebook/notebook.py
315+
:language: python
316+
:linenos:
317+
318+
Create, query, retry, and cancel notebook executions.
319+
320+
.. literalinclude:: ../examples/notebook/notebook_execution.py
288321
:language: python
289322
:linenos:

examples/notebook/notebook.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from nisystemlink.clients.core import HttpConfiguration
2+
from nisystemlink.clients.notebook import NotebookClient
3+
from nisystemlink.clients.notebook.models import NotebookMetadata, QueryNotebookRequest
4+
5+
# Setup the server configuration to point to your instance of SystemLink Enterprise
6+
server_configuration = HttpConfiguration(
7+
server_uri="https://yourserver.yourcompany.com",
8+
api_key="YourAPIKeyGeneratedFromSystemLink",
9+
)
10+
client = NotebookClient(configuration=server_configuration)
11+
12+
# Create a notebook with metadata and content
13+
metadata = NotebookMetadata(
14+
name="Example Notebook",
15+
parameters={"param1": "value1"},
16+
properties={"property1": "value1"},
17+
)
18+
19+
with open("example.ipynb", "rb") as file:
20+
notebook_response = client.create_notebook(metadata=metadata, content=file)
21+
22+
# Get the notebook by ID
23+
notebook = client.get_notebook("your_notebook_id")
24+
25+
# Update the notebook with new metadata and content
26+
metadata = NotebookMetadata(
27+
name="Updated Example Notebook",
28+
parameters={"param1": "value2"},
29+
properties={"property1": "value2"},
30+
)
31+
32+
with open("example_updated.ipynb", "rb") as file:
33+
notebook_response = client.update_notebook(
34+
id="your_notebook_id",
35+
metadata=metadata,
36+
content=file,
37+
)
38+
39+
# Get notebook content by ID
40+
notebook_content = client.get_notebook_content("your_notebook_id")
41+
42+
# Query notebook by name
43+
query_request = QueryNotebookRequest(
44+
filter='name="Example Notebook"',
45+
)
46+
47+
query_response = client.query_notebooks(query_request)
48+
49+
# Query notebooks by take
50+
query_request = QueryNotebookRequest(take=2)
51+
query_response = client.query_notebooks(query_request)
52+
53+
query_request = QueryNotebookRequest(
54+
continuation_token=query_response.continuation_token,
55+
take=1,
56+
)
57+
query_response = client.query_notebooks(query_request)
58+
59+
# Delete the notebook by ID
60+
client.delete_notebook("your_notebook_id")
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from nisystemlink.clients.core import HttpConfiguration
2+
from nisystemlink.clients.notebook import NotebookClient
3+
from nisystemlink.clients.notebook.models import (
4+
CreateExecutionRequest,
5+
ExecutionField,
6+
ExecutionPriority,
7+
ExecutionResourceProfile,
8+
ExecutionSortField,
9+
ExecutionStatus,
10+
QueryExecutionsRequest,
11+
ReportSettings,
12+
ReportType,
13+
)
14+
15+
# Setup the server configuration to point to your instance of SystemLink Enterprise
16+
server_configuration = HttpConfiguration(
17+
server_uri="https://yourserver.yourcompany.com",
18+
api_key="YourAPIKeyGeneratedFromSystemLink",
19+
)
20+
client = NotebookClient(configuration=server_configuration)
21+
22+
23+
# Create a notebook execution
24+
execution_request = CreateExecutionRequest(
25+
notebook_id="your_notebook_id",
26+
parameters={"param1": "value1"},
27+
workspace_id="your_workspace_id",
28+
timeout=300,
29+
result_cache_period=3600,
30+
report_settings=ReportSettings(
31+
format=ReportType.HTML,
32+
exclude_code=False,
33+
),
34+
client_requests_id="your_client_request_id",
35+
priority=ExecutionPriority.HIGH,
36+
resource_profile=ExecutionResourceProfile.DEFAULT,
37+
)
38+
39+
# Pass the list of execution requests to the create_executions method
40+
create_execution_response = client.create_executions([execution_request])
41+
42+
# Get the execution by ID
43+
execution = client.get_execution_by_id("your_execution_id")
44+
45+
# Query executions
46+
query_request = QueryExecutionsRequest(
47+
filter=f"(status = {ExecutionStatus.FAILED.value}))",
48+
order_by=ExecutionSortField.COMPLETED_AT,
49+
descending=True,
50+
projection=[
51+
ExecutionField.ID,
52+
ExecutionField.NOTEBOOK_ID,
53+
ExecutionField.STATUS,
54+
],
55+
)
56+
57+
query_executions_response = client.query_executions(query_request)
58+
59+
# Cancel execution
60+
cancel_execution_response = client.cancel_executions(["your_execution_id"])
61+
62+
# Retry execution
63+
retry_execution_response = client.retry_executions(["your_execution_id"])
64+
65+
# Create execution from existing one
66+
run_new = client.create_executions_from_existing(["your_execution_id"])

nisystemlink/clients/core/_uplink/_methods.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ def decorator(func: F) -> F:
4040
return decorator
4141

4242

43+
def put(path: str, args: Optional[Sequence[Any]] = None) -> Callable[[F], F]:
44+
"""Annotation for a PUT request with a JSON request body. If args is not
45+
specified, defaults to a single argument that represents the request body.
46+
"""
47+
48+
def decorator(func: F) -> F:
49+
return json(commands.put(path, args=args or (Body,))(func)) # type: ignore
50+
51+
return decorator
52+
53+
4354
def patch(path: str, args: Optional[Sequence[Any]] = None) -> Callable[[F], F]:
4455
"""Annotation for a PATCH request with a JSON request body."""
4556

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from ._notebook_client import NotebookClient
2+
3+
# flake8: noqa

0 commit comments

Comments
 (0)