From 227c4cc5c006707192cdf84851907d6912b1c2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Sat, 13 Jul 2024 11:03:37 +0200 Subject: [PATCH] Add `port` keyword argument to `notebook` and `jupyterlab` --- Project.toml | 2 +- src/jupyter.jl | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Project.toml b/Project.toml index f014614e..88aca27b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "IJulia" uuid = "7073ff75-c697-5162-941a-fcdaad2a7d2a" -version = "1.25.0" +version = "1.26.0" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" diff --git a/src/jupyter.jl b/src/jupyter.jl index 7331f135..5c53337e 100644 --- a/src/jupyter.jl +++ b/src/jupyter.jl @@ -9,7 +9,7 @@ import Conda isyes(s) = isempty(s) || lowercase(strip(s)) in ("y", "yes") -function find_jupyter_subcommand(subcommand::AbstractString) +function find_jupyter_subcommand(subcommand::AbstractString, port::Union{Nothing,Int}=nothing) jupyter = JUPYTER if jupyter == "jupyter" || jupyter == "jupyter.exe" # look in PATH jupyter = Sys.which(exe("jupyter")) @@ -26,7 +26,8 @@ function find_jupyter_subcommand(subcommand::AbstractString) end end - cmd = `$jupyter $subcommand` + port_flag = !isnothing(port) ? `--port=$(port)` : `` + cmd = `$(jupyter) $(subcommand) $(port_flag)` # fails in Windows if jupyter directory is not in PATH (jupyter/jupyter_core#62) pathsep = Sys.iswindows() ? ';' : ':' @@ -69,7 +70,7 @@ function launch(cmd, dir, detached) end """ - notebook(; dir=homedir(), detached=false) + notebook(; dir=homedir(), detached=false, port::Union{Nothing,Int}=nothing) The `notebook()` function launches the Jupyter notebook, and is equivalent to running `jupyter notebook` at the operating-system @@ -90,22 +91,28 @@ the `jupyter notebook` will launch in the background, and will continue running even after you quit Julia. (The only way to stop Jupyter will then be to kill it in your operating system's process manager.) + +When the optional keyword `port` is not `nothing`, open the notebook on the +given port number. + +For launching a JupyterLab instance, see [`IJulia.jupyterlab()`](@ref). """ -function notebook(; dir=homedir(), detached=false) +function notebook(; dir=homedir(), detached=false, port::Union{Nothing,Int}=nothing) inited && error("IJulia is already running") - notebook = find_jupyter_subcommand("notebook") + notebook = find_jupyter_subcommand("notebook", port) + @show notebook return launch(notebook, dir, detached) end """ - jupyterlab(; dir=homedir(), detached=false) + jupyterlab(; dir=homedir(), detached=false, port::Union{Nothing,Int}=nothing) -Similar to `IJulia.notebook()` but launches JupyterLab instead +Similar to [`IJulia.notebook()`](@ref) but launches JupyterLab instead of the Jupyter notebook. """ -function jupyterlab(; dir=homedir(), detached=false) +function jupyterlab(; dir=homedir(), detached=false, port::Union{Nothing,Int}=nothing) inited && error("IJulia is already running") - lab = find_jupyter_subcommand("lab") + lab = find_jupyter_subcommand("lab", port) jupyter = first(lab) if dirname(jupyter) == abspath(Conda.SCRIPTDIR) && !Sys.isexecutable(exe(jupyter, "-lab")) &&