Skip to content

Commit 1ed733f

Browse files
authored
Merge pull request #1118 from JuliaLang/mg/port
Add `port` keyword argument to `notebook` and `jupyterlab`
2 parents 60568fa + 227c4cc commit 1ed733f

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "IJulia"
22
uuid = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
3-
version = "1.25.0"
3+
version = "1.26.0"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

src/jupyter.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Conda
99

1010
isyes(s) = isempty(s) || lowercase(strip(s)) in ("y", "yes")
1111

12-
function find_jupyter_subcommand(subcommand::AbstractString)
12+
function find_jupyter_subcommand(subcommand::AbstractString, port::Union{Nothing,Int}=nothing)
1313
jupyter = JUPYTER
1414
if jupyter == "jupyter" || jupyter == "jupyter.exe" # look in PATH
1515
jupyter = Sys.which(exe("jupyter"))
@@ -26,7 +26,8 @@ function find_jupyter_subcommand(subcommand::AbstractString)
2626
end
2727
end
2828

29-
cmd = `$jupyter $subcommand`
29+
port_flag = !isnothing(port) ? `--port=$(port)` : ``
30+
cmd = `$(jupyter) $(subcommand) $(port_flag)`
3031

3132
# fails in Windows if jupyter directory is not in PATH (jupyter/jupyter_core#62)
3233
pathsep = Sys.iswindows() ? ';' : ':'
@@ -69,7 +70,7 @@ function launch(cmd, dir, detached)
6970
end
7071

7172
"""
72-
notebook(; dir=homedir(), detached=false)
73+
notebook(; dir=homedir(), detached=false, port::Union{Nothing,Int}=nothing)
7374
7475
The `notebook()` function launches the Jupyter notebook, and is
7576
equivalent to running `jupyter notebook` at the operating-system
@@ -90,22 +91,28 @@ the `jupyter notebook` will launch in the background, and will
9091
continue running even after you quit Julia. (The only way to
9192
stop Jupyter will then be to kill it in your operating system's
9293
process manager.)
94+
95+
When the optional keyword `port` is not `nothing`, open the notebook on the
96+
given port number.
97+
98+
For launching a JupyterLab instance, see [`IJulia.jupyterlab()`](@ref).
9399
"""
94-
function notebook(; dir=homedir(), detached=false)
100+
function notebook(; dir=homedir(), detached=false, port::Union{Nothing,Int}=nothing)
95101
inited && error("IJulia is already running")
96-
notebook = find_jupyter_subcommand("notebook")
102+
notebook = find_jupyter_subcommand("notebook", port)
103+
@show notebook
97104
return launch(notebook, dir, detached)
98105
end
99106

100107
"""
101-
jupyterlab(; dir=homedir(), detached=false)
108+
jupyterlab(; dir=homedir(), detached=false, port::Union{Nothing,Int}=nothing)
102109
103-
Similar to `IJulia.notebook()` but launches JupyterLab instead
110+
Similar to [`IJulia.notebook()`](@ref) but launches JupyterLab instead
104111
of the Jupyter notebook.
105112
"""
106-
function jupyterlab(; dir=homedir(), detached=false)
113+
function jupyterlab(; dir=homedir(), detached=false, port::Union{Nothing,Int}=nothing)
107114
inited && error("IJulia is already running")
108-
lab = find_jupyter_subcommand("lab")
115+
lab = find_jupyter_subcommand("lab", port)
109116
jupyter = first(lab)
110117
if dirname(jupyter) == abspath(Conda.SCRIPTDIR) &&
111118
!Sys.isexecutable(exe(jupyter, "-lab")) &&

0 commit comments

Comments
 (0)