|
| 1 | +from .discussion import DSSObjectDiscussions |
| 2 | +from .utils import DSSTaggableObjectListItem |
| 3 | + |
| 4 | +class DSSJupyterNotebookListItem(DSSTaggableObjectListItem): |
| 5 | + """An item in a list of Jupyter notebooks. Do not instantiate this class, use :meth:`dataikuapi.dss.project.DSSProject.list_jupyter_notebooks`""" |
| 6 | + def __init__(self, client, data): |
| 7 | + super(DSSJupyterNotebookListItem, self).__init__(data) |
| 8 | + self.client = client |
| 9 | + |
| 10 | + def to_notebook(self): |
| 11 | + """Gets the :class:`DSSJupyterNotebook` corresponding to this notebook""" |
| 12 | + return DSSJupyterNotebook(self.client, self._data["projectKey"], self._data["name"]) |
| 13 | + |
| 14 | + @property |
| 15 | + def name(self): |
| 16 | + return self._data["name"] |
| 17 | + @property |
| 18 | + def language(self): |
| 19 | + return self._data["language"] |
| 20 | + @property |
| 21 | + def kernel_spec(self): |
| 22 | + return self._data["kernelSpec"] |
| 23 | + |
| 24 | +class DSSJupyterNotebook(object): |
| 25 | + def __init__(self, client, project_key, notebook_name): |
| 26 | + self.client = client |
| 27 | + self.project_key = project_key |
| 28 | + self.notebook_name = notebook_name |
| 29 | + |
| 30 | + def unload(self, session_id=None): |
| 31 | + """ |
| 32 | + Stop this Jupyter notebook and release its resources |
| 33 | + """ |
| 34 | + sessions = self.get_sessions() |
| 35 | + if sessions is None: |
| 36 | + raise Exception("Notebook isn't running") |
| 37 | + if len(sessions) == 0: |
| 38 | + raise Exception("Notebook isn't running") |
| 39 | + if session_id is None: |
| 40 | + if len(sessions) > 1: |
| 41 | + raise Exception("Several sessions of the notebook are running, choose one") |
| 42 | + else: |
| 43 | + session_id = sessions[0].get('sessionId', None) |
| 44 | + return self.client._perform_json("DELETE", |
| 45 | + "/projects/%s/jupyter-notebooks/%s/sessions/%s" % (self.project_key, self.notebook_name, session_id)) |
| 46 | + |
| 47 | + def get_sessions(self, as_objects=False): |
| 48 | + """ |
| 49 | + Get the list of running sessions of this Jupyter notebook |
| 50 | +
|
| 51 | + :param boolean as_objects: if True, each returned item will be a :class:`dataikuapi.dss.notebook.DSSNotebookSession` |
| 52 | + :rtype: list of :class:`dataikuapi.dss.notebook.DSSNotebookSession` or list of dict |
| 53 | + """ |
| 54 | + sessions = self.client._perform_json("GET", |
| 55 | + "/projects/%s/jupyter-notebooks/%s/sessions" % (self.project_key, self.notebook_name)) |
| 56 | + |
| 57 | + if as_objects: |
| 58 | + return [DSSNotebookSession(self.client, session) for session in sessions] |
| 59 | + else: |
| 60 | + return sessions |
| 61 | + |
| 62 | + def get_content(self): |
| 63 | + """ |
| 64 | + Get the content of this Jupyter notebook (metadata, cells, nbformat) |
| 65 | + """ |
| 66 | + raw_content = self.client._perform_json("GET", "/projects/%s/jupyter-notebooks/%s" % (self.project_key, self.notebook_name)) |
| 67 | + return DSSNotebookContent(self.client, self.project_key, self.notebook_name, raw_content) |
| 68 | + |
| 69 | + def delete(self): |
| 70 | + """ |
| 71 | + Delete this Jupyter notebook and stop all of its active sessions. |
| 72 | + """ |
| 73 | + return self.client._perform_json("DELETE", |
| 74 | + "/projects/%s/jupyter-notebooks/%s" % (self.project_key, self.notebook_name)) |
| 75 | + |
| 76 | + ######################################################## |
| 77 | + # Discussions |
| 78 | + ######################################################## |
| 79 | + def get_object_discussions(self): |
| 80 | + """ |
| 81 | + Get a handle to manage discussions on the notebook |
| 82 | +
|
| 83 | + :returns: the handle to manage discussions |
| 84 | + :rtype: :class:`dataikuapi.discussion.DSSObjectDiscussions` |
| 85 | + """ |
| 86 | + return DSSObjectDiscussions(self.client, self.project_key, "JUPYTER_NOTEBOOK", self.notebook_name) |
| 87 | + |
| 88 | +class DSSNotebookContent(object): |
| 89 | + """ |
| 90 | + Content of a Jupyter Notebook. Do not create this directly, use :meth:`DSSJupyterNotebook.get_content` |
| 91 | + """ |
| 92 | + |
| 93 | + """ |
| 94 | + A Python/R/Scala notebook on the DSS instance |
| 95 | + """ |
| 96 | + def __init__(self, client, project_key, notebook_name, content): |
| 97 | + self.client = client |
| 98 | + self.project_key = project_key |
| 99 | + self.notebook_name = notebook_name |
| 100 | + self.content = content |
| 101 | + |
| 102 | + def get_raw(self): |
| 103 | + """ |
| 104 | + Get the content of this Jupyter notebook (metadata, cells, nbformat) |
| 105 | + :rtype: a dict containing the full content of a notebook |
| 106 | + """ |
| 107 | + return self.content |
| 108 | + |
| 109 | + def get_metadata(self): |
| 110 | + """ |
| 111 | + Get the metadata associated to this Jupyter notebook |
| 112 | + :rtype: dict with metadata |
| 113 | + """ |
| 114 | + return self.content["metadata"] |
| 115 | + |
| 116 | + def get_cells(self): |
| 117 | + """ |
| 118 | + Get the cells associated to this Jupyter notebook |
| 119 | + :rtype: list of cells |
| 120 | + """ |
| 121 | + return self.content["cells"] |
| 122 | + |
| 123 | + def save(self): |
| 124 | + """ |
| 125 | + Save the content of this Jupyter notebook |
| 126 | + """ |
| 127 | + return self.client._perform_json("PUT", |
| 128 | + "/projects/%s/jupyter-notebooks/%s" % (self.project_key, self.notebook_name), |
| 129 | + body=self.content) |
| 130 | + |
| 131 | +class DSSNotebookSession(object): |
| 132 | + """ |
| 133 | + Metadata associated to the session of a Jupyter Notebook. Do not create this directly, use :meth:`DSSJupyterNotebook.get_sessions()` |
| 134 | + """ |
| 135 | + |
| 136 | + def __init__(self, client, session): |
| 137 | + self.client = client |
| 138 | + self.project_key = session.get("projectKey") |
| 139 | + self.notebook_name = session.get("notebookName") |
| 140 | + self.session_creator = session.get("sessionCreator") |
| 141 | + self.session_creator_display_name = session.get("sessionCreatorDisplayName") |
| 142 | + self.session_unix_owner = session.get("sessionUnixOwner") |
| 143 | + self.session_id = session.get("sessionId") |
| 144 | + self.kernel_id = session.get("kernelId") |
| 145 | + self.kernel_pid = session.get("kernelPid") |
| 146 | + self.kernel_connections = session.get("kernelConnections") |
| 147 | + self.kernel_last_activity_time = session.get("kernelLastActivityTime") |
| 148 | + self.kernel_execution_state = session.get("kernelExecutionState") |
| 149 | + self.session_start_time = session.get("sessionStartTime") |
| 150 | + |
| 151 | + |
| 152 | + def unload(self): |
| 153 | + """ |
| 154 | + Stop this Jupyter notebook and release its resources |
| 155 | + """ |
| 156 | + return self.client._perform_json("DELETE", |
| 157 | + "/projects/%s/jupyter-notebooks/%s/sessions/%s" % (self.project_key, self.notebook_name, self.session_id)) |
0 commit comments