Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to connect to pre-existing kernel #60

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/react/src/jupyter/Jupyter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type JupyterProps = {
lite: boolean;
startDefaultKernel: boolean;
defaultKernelName: string;
selectRunningKernel?: boolean;
injectableStore?: Store | any;
collaborative?: boolean;
jupyterServerHttpUrl?: string;
Expand Down Expand Up @@ -80,6 +81,7 @@ export const Jupyter = (props: JupyterProps) => {
lite={lite}
startDefaultKernel={startDefaultKernel}
defaultKernelName={defaultKernelName}
selectRunningKernel={props?.selectRunningKernel ?? false}
baseUrl={getJupyterServerHttpUrl()}
wsUrl={getJupyterServerWsUrl()}
injectableStore={props.injectableStore || injectableStore}
Expand All @@ -97,6 +99,7 @@ Jupyter.defaultProps = {
lite: false,
defaultKernelName: 'python',
startDefaultKernel: true,
selectRunningKernel: false,
collaborative: false,
terminals: false,
}
Expand Down
9 changes: 6 additions & 3 deletions packages/react/src/jupyter/JupyterContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type JupyterContextType = {
kernelManager?: KernelManager,
defaultKernel?: Kernel,
startDefaultKernel: boolean,
selectRunningKernel?: boolean,
variant: string;
setVariant: (value: string) => void;
baseUrl: string;
Expand Down Expand Up @@ -68,6 +69,7 @@ type JupyterContextProps = {
wsUrl: string;
lite: boolean;
startDefaultKernel: boolean,
selectRunningKernel: boolean,
defaultKernelName: string,
injectableStore: any;
};
Expand Down Expand Up @@ -101,11 +103,12 @@ export const JupyterContextProvider: React.FC<{
lite: boolean,
startDefaultKernel: boolean,
defaultKernelName: string,
selectRunningKernel: boolean,
variant: string,
baseUrl: string,
wsUrl: string,
injectableStore: any
}> = ({children, lite, startDefaultKernel, defaultKernelName, variant, baseUrl, wsUrl, injectableStore }: JupyterContextProps) => {
}> = ({children, lite, startDefaultKernel, defaultKernelName, selectRunningKernel, variant, baseUrl, wsUrl, injectableStore }: JupyterContextProps) => {
const [_, setVariant] = useState('default');
const [serverSettings] = useState<ServerConnection.ISettings>(createServerSettings(baseUrl, wsUrl));
const [serviceManager, setServiceManager] = useState<ServiceManager>();
Expand All @@ -120,7 +123,7 @@ export const JupyterContextProvider: React.FC<{
kernelManager.ready.then(() => {
console.log('Kernel Manager is ready', kernelManager);
if (startDefaultKernel) {
const kernel = new Kernel({ kernelManager, kernelName: defaultKernelName });
const kernel = new Kernel({ kernelManager, kernelName: defaultKernelName, selectRunningKernel: selectRunningKernel });
kernel.getJupyterKernel().then(k => {
console.log(`Kernel started with session client_id:id ${k.clientId}:${k.id}`);
k.info.then(info => {
Expand All @@ -145,7 +148,7 @@ export const JupyterContextProvider: React.FC<{
kernelManager.ready.then(() => {
console.log('Kernel Manager is ready', kernelManager);
if (startDefaultKernel) {
const kernel = new Kernel({ kernelManager, kernelName: defaultKernelName });
const kernel = new Kernel({ kernelManager, kernelName: defaultKernelName, selectRunningKernel: selectRunningKernel });
kernel.getJupyterKernel().then(k => {
console.log(`Kernel started with session client_id:id ${k.clientId}:${k.id}`);
k.info.then(info => {
Expand Down
28 changes: 18 additions & 10 deletions packages/react/src/jupyter/services/kernel/Kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { UUID } from '@lumino/coreutils';
export type IKernelProps = {
kernelManager: KernelManager;
kernelName: string;
selectRunningKernel?: boolean;
}

export class Kernel {
Expand All @@ -18,10 +19,10 @@ export class Kernel {
public constructor(props: IKernelProps) {
this._kernelManager = props.kernelManager;
this._kernelName = props.kernelName;
this._jupyterKernel = this.requestJupyterKernel(); // Request the effective Jupyter Kernel.
this._jupyterKernel = this.requestJupyterKernel(props?.selectRunningKernel); // Request the effective Jupyter Kernel.
}

private async requestJupyterKernel(): Promise<JupyterKernel.IKernelConnection> {
private async requestJupyterKernel(selectRunningKernel?: boolean): Promise<JupyterKernel.IKernelConnection> {
await this._kernelManager.ready;
const sessionManager = new SessionManager({
kernelManager: this._kernelManager,
Expand All @@ -30,14 +31,21 @@ export class Kernel {
});
await sessionManager.ready;
const randomName = UUID.uuid4();
this._session = await sessionManager.startNew({
path: randomName,
name: randomName,
type: this._kernelName,
kernel: {
name: this._kernelName,
},
});

if (selectRunningKernel) {
console.log('Attempting to use pre-exisitng kernel')
const runningModel = sessionManager.running().next().value;
this._session = sessionManager.connectTo({model: runningModel});
} else {
this._session = await sessionManager.startNew({
path: randomName,
name: randomName,
type: this._kernelName,
kernel: {
name: this._kernelName,
},
});
}
this._info = await this._session.kernel!.info;
this._id = this._session.kernel!.id;
return this._session.kernel!;
Expand Down