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

Add port keyword argument to notebook and jupyterlab #1118

Merged
merged 1 commit into from
Oct 7, 2024
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
25 changes: 16 additions & 9 deletions src/jupyter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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() ? ';' : ':'
Expand Down Expand Up @@ -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
Expand All @@ -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")) &&
Expand Down
Loading