jupyter_websocket_client/
client.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use url::Url;

use crate::websocket::JupyterWebSocket;

pub struct RemoteServer {
    pub base_url: String,
    pub token: String,
}

// Only `id` is used right now, but other fields will be useful when pulling up a listing later
#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Kernel {
    pub id: String,
    pub name: String,
    pub last_activity: String,
    pub execution_state: String,
    pub connections: u64,
}

#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Session {
    pub id: String,
    pub path: String,
    pub name: String,
    #[serde(rename = "type")]
    pub session_type: String,
    pub kernel: Kernel,
}

#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct NewSession {
    pub path: String,
    pub name: Option<String>,
}

#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct KernelSpec {
    pub name: String,
    pub spec: KernelSpecFile,
    pub resources: std::collections::HashMap<String, String>,
}

#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct KernelLaunchRequest {
    pub name: String,
}

#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct KernelSpecFile {
    pub argv: Vec<String>,
    pub display_name: String,
    pub language: String,
    pub codemirror_mode: Option<String>,
    pub env: Option<std::collections::HashMap<String, String>>,
    pub help_links: Option<Vec<HelpLink>>,
}

#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct HelpLink {
    pub text: String,
    pub url: String,
}

#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct KernelSpecsResponse {
    pub default: String,
    pub kernelspecs: std::collections::HashMap<String, KernelSpec>,
}

fn api_url(base_url: &str, path: &str) -> String {
    format!(
        "{}/api/{}",
        base_url.trim_end_matches('/'),
        path.trim_start_matches('/')
    )
}

impl RemoteServer {
    pub fn from_url(url: &str) -> Result<Self> {
        let parsed_url = Url::parse(url).context("Failed to parse Jupyter URL")?;
        let base_url = format!(
            "{}://{}{}{}",
            parsed_url.scheme(),
            parsed_url.host_str().unwrap_or("localhost"),
            parsed_url
                .port()
                .map(|p| format!(":{}", p))
                .unwrap_or_default(),
            parsed_url.path().trim_end_matches("/tree")
        );

        let token = parsed_url
            .query_pairs()
            .find(|(key, _)| key == "token")
            .map(|(_, value)| value.into_owned())
            .ok_or_else(|| anyhow::anyhow!("Token not found in URL"))?;

        Ok(Self { base_url, token })
    }

    pub fn api_url(&self, path: &str) -> String {
        api_url(&self.base_url, path)
    }

    /// Connect to a kernel by ID
    ///
    /// ```rust
    /// use jupyter_websocket_client::RemoteServer;
    ///
    /// use jupyter_protocol::{KernelInfoRequest, JupyterMessageContent};
    ///
    /// // Import the sink and stream extensions to allow splitting the socket into a writer and reader pair
    /// use futures::{SinkExt as _, StreamExt as _};
    ///
    /// pub async fn connect_kernel() -> anyhow::Result<()> {
    ///     let server = RemoteServer::from_url(
    ///         "http://127.0.0.1:8888/lab?token=f487535a46268da4a0752c0e162c873b721e33a9e6ec8390"
    ///     )?;
    ///
    ///     // You'll need to launch a kernel and get a kernel ID using your own HTTP
    ///     // request library
    ///     let kernel_id = "1057-1057-1057-1057";
    ///
    ///     let kernel_socket = server.connect_to_kernel(kernel_id).await?;
    ///
    ///     let (mut w, mut r) = kernel_socket.split();
    ///
    ///     w.send(KernelInfoRequest {}.into()).await?;
    ///
    ///     while let Some(response) = r.next().await.transpose()? {
    ///         match response.content {
    ///             JupyterMessageContent::KernelInfoReply(kernel_info_reply) => {
    ///                 println!("Received kernel_info_reply");
    ///                 println!("{:?}", kernel_info_reply);
    ///                 break;
    ///             }
    ///             other => {
    ///                 println!("Received");
    ///                 println!("{:?}", other);
    ///             }
    ///         }
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn connect_to_kernel(&self, kernel_id: &str) -> Result<JupyterWebSocket> {
        let ws_url = format!(
            "{}?token={}",
            api_url(&self.base_url, &format!("kernels/{}/channels", kernel_id))
                .replace("http", "ws"),
            self.token
        );

        let jupyter_websocket = crate::websocket::connect(&ws_url).await?;
        Ok(jupyter_websocket)
    }
}

// pub async fn start_kernel(&self, name: &str) -> Result<String> {
//     let kernels_url = self.api_url("kernels");
//     let response = self
//         .client
//         .post(&kernels_url)
//         .header("Authorization", format!("Token {}", self.token))
//         .json(&KernelLaunch {
//             name: name.to_string(),
//         })
//         .send()
//         .await
//         .context("Failed to send kernel start request")?;

//     let kernel = response
//         .json::<Kernel>()
//         .await
//         .context("Failed to parse kernel info")?;

//     eprintln!("Kernel info: {:?}", kernel);
//     Ok(kernel.id)
// }
// pub async fn connect_to_kernel(&self, kernel_id: &str) -> Result<JupyterWebSocket> {
//     let ws_url = format!(
//         "{}?token={}",
//         self.api_url(&format!("kernels/{}/channels", kernel_id))
//             .replace("http", "ws"),
//         self.token
//     );

//     let jupyter_websocket = crate::websocket::connect(&ws_url).await?;
//     Ok(jupyter_websocket)
// }
// pub async fn shutdown(&self, kernel_id: &str) -> Result<()> {
//     let kernels_url = self.api_url(&format!("kernels/{}", kernel_id));
//     let response = self
//         .client
//         .delete(&kernels_url)
//         .header("Authorization", format!("Token {}", self.token))
//         .send()
//         .await
//         .context("Failed to send shutdown request")?;
//     if response.status().is_success() {
//         Ok(())
//     } else {
//         anyhow::bail!("Failed to shut down kernel: {:?}", response.status())
//     }
// }
// pub async fn list_kernels(&self) -> Result<Vec<Kernel>> {
//     let url = self.api_url("kernels");
//     let response = self
//         .client
//         .get(&url)
//         .header("Authorization", format!("Token {}", self.token))
//         .send()
//         .await
//         .context("Failed to send list kernels request")?;

//     response
//         .json()
//         .await
//         .context("Failed to parse kernels list")
// }
// pub async fn list_sessions(&self) -> Result<Vec<Session>> {
//     let url = self.api_url("sessions");
//     let response = self
//         .client
//         .get(&url)
//         .header("Authorization", format!("Token {}", self.token))
//         .send()
//         .await
//         .context("Failed to send list sessions request")?;

//     response
//         .json()
//         .await
//         .context("Failed to parse sessions list")
// }
// pub async fn list_kernel_specs(&self) -> Result<KernelSpecsResponse> {
//     let url = self.api_url("kernelspecs");
//     let response = self
//         .client
//         .get(&url)
//         .header("Authorization", format!("Token {}", self.token))
//         .send()
//         .await
//         .context("Failed to send list kernel specs request")?;

//     response
//         .json()
//         .await
//         .context("Failed to parse kernel specs")
// }