Skip to content

Commit 7071318

Browse files
Rework labextension based on PR comments
Move RunningServerProxyApp to a separate file Use lab transalation capabilities for complex strings Use URLExt package to manipulate URLs
1 parent d358856 commit 7071318

File tree

2 files changed

+68
-46
lines changed

2 files changed

+68
-46
lines changed

labextension/src/index.ts

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,14 @@ import {
44
ILayoutRestorer,
55
} from "@jupyterlab/application";
66
import { ILauncher } from "@jupyterlab/launcher";
7-
import { PageConfig } from "@jupyterlab/coreutils";
8-
import { IRunningSessionManagers, IRunningSessions } from "@jupyterlab/running";
7+
import { PageConfig, URLExt } from "@jupyterlab/coreutils";
8+
import { IRunningSessionManagers } from "@jupyterlab/running";
9+
import { ISettingRegistry } from '@jupyterlab/settingregistry';
10+
import { ITranslator, TranslationBundle } from '@jupyterlab/translation';
911
import { IFrame, MainAreaWidget, WidgetTracker } from "@jupyterlab/apputils";
10-
import { LabIcon } from "@jupyterlab/ui-components";
1112
import { ServerProxyManager } from "./manager";
1213
import { IModel as IServerProxyModel } from "./serverproxy";
13-
import serverProxyAppSvgstr from "../style/icons/proxy.svg";
14-
15-
export const ServerProxyAppIcon = new LabIcon({
16-
name: "server-proxy:proxyAppIcon",
17-
svgstr: serverProxyAppSvgstr,
18-
});
19-
20-
namespace CommandIDs {
21-
export const open = "running-server-proxy:open";
22-
}
14+
import { RunningServerProxyApp, CommandIDs } from "./running";
2315

2416
function newServerProxyWidget(
2517
id: string,
@@ -56,42 +48,20 @@ function addRunningSessionManager(
5648
managers: IRunningSessionManagers,
5749
app: JupyterFrontEnd,
5850
manager: ServerProxyManager,
51+
trans: TranslationBundle
5952
): void {
6053
managers.add({
6154
name: "Server Proxy Apps",
6255
running: () =>
6356
Array.from(manager.running()).map(
64-
(model) => new RunningServerProxyApp(model),
57+
(model) => new RunningServerProxyApp(model, manager, app),
6558
),
6659
shutdownAll: () => manager.shutdownAll(),
6760
refreshRunning: () => manager.refreshRunning(),
6861
runningChanged: manager.runningChanged,
6962
shutdownAllConfirmationText:
70-
"Are you sure you want to close all server proxy applications?",
63+
trans.__("Are you sure you want to close all server proxy applications?")
7164
});
72-
73-
class RunningServerProxyApp implements IRunningSessions.IRunningItem {
74-
constructor(model: IServerProxyModel) {
75-
this._model = model;
76-
}
77-
open(): void {
78-
app.commands.execute(CommandIDs.open, { sp: this._model });
79-
}
80-
icon(): LabIcon {
81-
return ServerProxyAppIcon;
82-
}
83-
label(): string {
84-
return `${this._model.name}`;
85-
}
86-
labelTitle(): string {
87-
return `cmd: ${this._model.cmd}\nport: ${this._model.port}\nmanaged: ${this._model.managed}`;
88-
}
89-
shutdown(): Promise<void> {
90-
return manager.shutdown(this._model.name);
91-
}
92-
93-
private _model: IServerProxyModel;
94-
}
9565
}
9666

9767
/**
@@ -104,21 +74,28 @@ async function activate(
10474
app: JupyterFrontEnd,
10575
launcher: ILauncher,
10676
restorer: ILayoutRestorer,
77+
settingRegistry: ISettingRegistry,
78+
translator: ITranslator,
10779
sessions: IRunningSessionManagers | null,
10880
): Promise<void> {
81+
const trans = translator.load('jupyter-server-proxy');
82+
10983
// Fetch configured server processes from {base_url}/server-proxy/servers-info
11084
const response = await fetch(
111-
PageConfig.getBaseUrl() + "api/server-proxy/servers-info",
85+
URLExt.join(PageConfig.getBaseUrl(), "server-proxy/api/servers-info"),
11286
);
11387
if (!response.ok) {
11488
console.log(
115-
"Could not fetch metadata about registered servers. Make sure jupyter-server-proxy is installed.",
89+
trans.__("Could not fetch metadata about registered servers. Make sure jupyter-server-proxy is installed."),
11690
);
11791
console.log(response);
11892
return;
11993
}
12094
const data = await response.json();
12195

96+
// Load application settings
97+
const settings = await settingRegistry.load(extension.id);
98+
12299
const namespace = "server-proxy";
123100
const tracker = new WidgetTracker<MainAreaWidget<IFrame>>({
124101
namespace,
@@ -142,8 +119,8 @@ async function activate(
142119

143120
// Add server proxy session manager to running sessions
144121
if (sessions) {
145-
let manager = new ServerProxyManager();
146-
addRunningSessionManager(sessions, app, manager);
122+
let manager = new ServerProxyManager(trans, settings);
123+
addRunningSessionManager(sessions, app, manager, trans);
147124
}
148125

149126
commands.addCommand(command, {
@@ -178,7 +155,7 @@ async function activate(
178155
commands.addCommand(CommandIDs.open, {
179156
execute: (args) => {
180157
const model = args["sp"] as IServerProxyModel;
181-
const url = PageConfig.getBaseUrl() + model.url;
158+
const url = URLExt.join(PageConfig.getBaseUrl(), model.url);
182159
window.open(url, "_blank");
183160
return;
184161
},
@@ -189,8 +166,7 @@ async function activate(
189166
continue;
190167
}
191168

192-
const url =
193-
PageConfig.getBaseUrl() + server_process.launcher_entry.path_info;
169+
const url = URLExt.join(PageConfig.getBaseUrl(), server_process.launcher_entry.path_info);
194170
const title = server_process.launcher_entry.title;
195171
const newBrowserTab = server_process.new_browser_tab;
196172
const id = namespace + ":" + server_process.name;
@@ -221,7 +197,7 @@ async function activate(
221197
const extension: JupyterFrontEndPlugin<void> = {
222198
id: "@jupyterhub/jupyter-server-proxy:add-launcher-entries",
223199
autoStart: true,
224-
requires: [ILauncher, ILayoutRestorer],
200+
requires: [ILauncher, ILayoutRestorer, ISettingRegistry, ITranslator],
225201
optional: [IRunningSessionManagers],
226202
activate: activate,
227203
};

labextension/src/running.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { JupyterFrontEnd } from "@jupyterlab/application";
2+
import { IRunningSessions } from "@jupyterlab/running";
3+
import { LabIcon } from "@jupyterlab/ui-components";
4+
import { ServerProxyManager } from "./manager";
5+
import { IModel as IServerProxyModel } from "./serverproxy";
6+
import serverProxyAppSvgstr from "../style/icons/proxy.svg";
7+
8+
export const ServerProxyAppIcon = new LabIcon({
9+
name: "server-proxy:proxyAppIcon",
10+
svgstr: serverProxyAppSvgstr,
11+
});
12+
13+
export namespace CommandIDs {
14+
export const open = "server-proxy:refresh";
15+
}
16+
17+
export class RunningServerProxyApp implements IRunningSessions.IRunningItem {
18+
constructor(
19+
model: IServerProxyModel,
20+
manager: ServerProxyManager,
21+
app: JupyterFrontEnd
22+
) {
23+
this._model = model;
24+
this._manager = manager;
25+
this._app = app;
26+
}
27+
open(): void {
28+
this._app.commands.execute(CommandIDs.open, { "sp": this._model });
29+
}
30+
icon(): LabIcon {
31+
return ServerProxyAppIcon;
32+
}
33+
label(): string {
34+
return `${this._model.name}`;
35+
}
36+
labelTitle(): string {
37+
return `cmd: ${this._model.cmd}\nport: ${this._model.port}\nunix_socket: ${this._model.unix_socket}\nmanaged: ${this._model.managed}`;
38+
}
39+
shutdown(): Promise<void> {
40+
return this._manager.shutdown(this._model.name);
41+
}
42+
43+
private _model: IServerProxyModel;
44+
private _manager: ServerProxyManager;
45+
private _app: JupyterFrontEnd;
46+
}

0 commit comments

Comments
 (0)