add function to open notebook urls (#986)

* add function to open notebook urls

* Update lib/livebook/utils.ex

Co-authored-by: José Valim <jose.valim@gmail.com>

* Update lib/livebook/utils.ex

Co-authored-by: José Valim <jose.valim@gmail.com>

* open notebook with notebook_open_url/2

* Update server.ex

* format Livebook.Utils

* Update lib/livebook_cli/server.ex

Co-authored-by: José Valim <jose.valim@gmail.com>

* correct function doc

Co-authored-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
Sam 2022-02-07 18:09:23 +05:30 committed by GitHub
parent 5e82147db0
commit 494ab40ac8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View file

@ -387,6 +387,24 @@ defmodule Livebook.Utils do
|> URI.to_string()
end
@doc """
Returns a URL (including localhost) to open the given `url` as a notebook
iex> Livebook.Utils.notebook_open_url("https://example.com/foo.livemd")
"http://localhost:4002/open?path=https%3A%2F%2Fexample.com%2Ffoo.livemd"
iex> Livebook.Utils.notebook_open_url("https://my_host", "https://example.com/foo.livemd")
"https://my_host/open?path=https%3A%2F%2Fexample.com%2Ffoo.livemd"
"""
def notebook_open_url(base_url \\ LivebookWeb.Endpoint.access_struct_url(), url) do
base_url
|> URI.parse()
|> Map.replace!(:path, "/open")
|> append_query("path=#{URI.encode_www_form(url)}")
|> URI.to_string()
end
# TODO: On Elixir v1.14, use URI.append_query/2
def append_query(%URI{query: query} = uri, query_to_add) when query in [nil, ""] do
%{uri | query: query_to_add}

View file

@ -168,8 +168,7 @@ defmodule LivebookCLI.Server do
File.regular?(path) ->
base_url
|> append_path("open")
|> update_query(%{"path" => path})
|> Livebook.Utils.notebook_open_url(url_or_file_or_dir)
|> Livebook.Utils.browser_open()
File.dir?(path) ->