Skip to content

Commit

Permalink
add launbrowser on run (#190)
Browse files Browse the repository at this point in the history
Co-authored-by: Fons van der Plas <[email protected]>
  • Loading branch information
mgautam98 and fonsp authored Sep 9, 2020
1 parent 8fb14e8 commit d0ff39e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Pluto"
uuid = "c3e4b0f8-55cb-11ea-2926-15256bba5781"
license = "MIT"
authors = ["Fons van der Plas <[email protected]>", "Mikołaj Bochenski <[email protected]>"]
version = "0.11.13"
version = "0.11.14"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
13 changes: 7 additions & 6 deletions src/webserver/Configuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ end

# ServerSecurity(val::Bool) = ServerSecurity(val, val, val)

# THIS IS NOT USED YET
"(More options coming...)"
Base.@kwdef struct ServerConfiguration
host::AbstractString = "127.0.0.1"
port::Integer = 1234
skip_main_menu::Bool = false
show_file_system::Bool = true
root_url::Union{Nothing,String} = nothing
# host::AbstractString = "127.0.0.1"
# port::Union{Nothing,Integer} = nothing
launch_browser::Bool = true
# single_notebook_mode::Union{Nothing,Notebook} = nothing
# show_file_system::Bool = true
end

# ServerSecurity(val::Bool) = ServerSecurity(val)

45 changes: 38 additions & 7 deletions src/webserver/WebServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ function HTTP.closebody(http::HTTP.Stream{HTTP.Messages.Request,S}) where S <: I
end
end

# from https://github.com/JuliaLang/julia/pull/36425
function detectwsl()
Sys.islinux() &&
isfile("/proc/sys/kernel/osrelease") &&
occursin(r"Microsoft|WSL"i, read("/proc/sys/kernel/osrelease", String))
end

function open_in_default_browser(url::AbstractString)::Bool
try
if Sys.isapple()
Base.run(`open $url`)
true
elseif Sys.iswindows() || detectwsl()
Base.run(`cmd.exe /s /c start "" /b $url`)
true
elseif Sys.islinux()
Base.run(`xdg-open $url`)
true
else
false
end
catch ex
showerror(stderr, ex, catch_backtrace())
false
end
end

"""
run([host,] port=1234[; kwargs...])
Expand All @@ -36,18 +62,18 @@ Start Pluto! Are you excited? I am!
## Kwargs
- `launchbrowser`: launch browser directly. (not implemented yet)
- `configuration`: specifiy the `Pluto.ServerConfiguration` to change the HTTP server behavior.
- `session`: specifiy the `Pluto.ServerSession` to run the web server on.
- `security`: specifiy the `Pluto.ServerSecurity` options for the web server.
Different configurations are possible by creating a custom [`ServerSession`](@ref) or [`ServerSecurity`](@ref) object. Have a look at their documentation.
Different configurations are possible by creating a custom [`ServerConfiguration`](@ref), [`ServerSession`](@ref) or [`ServerSecurity`](@ref) object. Have a look at their documentation.
## Technobabble
This will start the static HTTP server and a WebSocket server. The server runs _synchronously_ (i.e. blocking call) on `http://[host]:[port]/`.
Pluto notebooks can be started from the main menu in the web browser.
"""
function run(host, port::Union{Nothing,Integer}=nothing; launchbrowser::Bool=false, session=ServerSession(), security=ServerSecurity(true))
function run(host, port::Union{Nothing,Integer}=nothing; configuration=ServerConfiguration(), session=ServerSession(), security=ServerSecurity(true))
pluto_router = http_router_for(session, security)

hostIP = parse(Sockets.IPAddr, host)
Expand Down Expand Up @@ -163,18 +189,23 @@ function run(host, port::Union{Nothing,Integer}=nothing; launchbrowser::Bool=fal
end
end

address = let
address = if configuration.root_url === nothing
hostPretty = (hostStr = string(hostIP)) == "127.0.0.1" ? "localhost" : hostStr
portPretty = Int(port)
"http://$(hostPretty):$(portPretty)/"
else
configuration.root_url
end
Sys.set_process_title("Pluto server - $address")
println("Go to $address to start writing ~ have fun!")

if configuration.launch_browser && open_in_default_browser(address)
println("Opening $address in your default browser... ~ have fun!")
else
println("Go to $address in your browser to start writing ~ have fun!")
end
println()
println("Press Ctrl+C in this terminal to stop Pluto")
println()

launchbrowser && @warn "Not implemented yet"

kill_server[] = () -> @sync begin
println("\n\nClosing Pluto... Restart Julia for a fresh session. \n\nHave a nice day! 🎈")
Expand Down

2 comments on commit d0ff39e

@fonsp
Copy link
Owner

@fonsp fonsp commented on d0ff39e Sep 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/21140

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.11.14 -m "<description of version>" d0ff39e6e406ba24ae66f90d95be39db8de3aa3a
git push origin v0.11.14

Please sign in to comment.