Skip to content

Commit 2385974

Browse files
aleDszhugobarauna
andauthored
Shows the apps banner from environment variable (#3144)
Co-authored-by: Hugo Baraúna <hugo.barauna@gmail.com>
1 parent a4e21a5 commit 2385974

9 files changed

Lines changed: 118 additions & 55 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ The following environment variables can be used to configure Livebook on boot:
182182
* `LIVEBOOK_APP_SERVICE_URL` - sets the application url to manage this
183183
Livebook instance within the cloud provider platform.
184184

185+
* `LIVEBOOK_APPS_BANNER` - sets the value to render at the top apps banner.
186+
185187
* `LIVEBOOK_APPS_PATH` - the directory with app notebooks. When set, the apps
186188
are deployed on Livebook startup with the persisted settings. Password-protected
187189
notebooks will receive a random password, unless `LIVEBOOK_APPS_PATH_PASSWORD`

config/config.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ config :livebook,
3232
allowed_uri_schemes: [],
3333
app_service_name: nil,
3434
app_service_url: nil,
35+
apps_banner: nil,
3536
authentication: :token,
3637
aws_credentials: false,
3738
feature_flags: [],

lib/livebook.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ defmodule Livebook do
266266
config :livebook, :agent_name, agent_name
267267
end
268268

269+
if format = Livebook.Config.apps_banner!("LIVEBOOK_APPS_BANNER") do
270+
config :livebook, :apps_banner, format
271+
end
272+
269273
if image_registry_url = Livebook.Config.image_registry_url!("LIVEBOOK_IMAGE_REGISTRY_URL") do
270274
config :livebook, :image_registry_url, image_registry_url
271275
end

lib/livebook/config.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ defmodule Livebook.Config do
142142
Path.join([tmp_dir, "livebook", app_version()])
143143
end
144144

145+
@doc """
146+
Returns the apps top banner.
147+
"""
148+
@spec apps_banner() :: String.t() | nil
149+
def apps_banner() do
150+
Application.get_env(:livebook, :apps_banner)
151+
end
152+
145153
@doc """
146154
Returns the apps path.
147155
"""
@@ -733,6 +741,16 @@ defmodule Livebook.Config do
733741
end
734742
end
735743

744+
@doc """
745+
Parses the apps banner from env.
746+
"""
747+
def apps_banner!(env) do
748+
case System.get_env(env) do
749+
value when value in ["", nil] -> nil
750+
value -> value
751+
end
752+
end
753+
736754
@doc """
737755
Parses and validates allowed URI schemes from env.
738756
"""

lib/livebook_web/components/app_components.ex

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,27 @@ defmodule LivebookWeb.AppComponents do
1212
"""
1313
end
1414

15+
@doc """
16+
Renders the apps banner.
17+
18+
## Examples
19+
20+
<.apps_banner value="MyAppsBanner" />
21+
"""
22+
attr :value, :string, default: nil
23+
24+
def apps_banner(assigns) do
25+
~H"""
26+
<div
27+
:if={@value}
28+
id="apps-banner"
29+
class="w-full bg-gray-800 text-xs text-gray-400 text-center py-1 tracking-wider uppercase"
30+
>
31+
{@value}
32+
</div>
33+
"""
34+
end
35+
1536
@doc """
1637
Renders app status with indicator.
1738
"""

lib/livebook_web/live/app_live.ex

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule LivebookWeb.AppLive do
3030
Livebook.Teams.Broadcasts.subscribe(:app_deployments)
3131
end
3232

33-
{:ok, assign(socket, app: app)}
33+
{:ok, assign(socket, app: app, apps_banner: Livebook.Config.apps_banner())}
3434
else
3535
{:ok, pid} = Livebook.Apps.fetch_pid(slug)
3636
session_id = Livebook.App.get_session_id(pid, user: socket.assigns.current_user)
@@ -41,13 +41,12 @@ defmodule LivebookWeb.AppLive do
4141
@impl true
4242
def render(assigns) when assigns.app_authenticated? and assigns.app_authorized? do
4343
~H"""
44+
<.apps_banner value={@apps_banner} />
4445
<div class="h-full relative overflow-y-auto px-4 md:px-20">
46+
<div class="absolute right-8 md:left-4 top-3.5 w-10 h-10">
47+
<img src={~p"/images/logo.png"} height="40" width="40" alt="logo livebook" />
48+
</div>
4549
<div class="w-full max-w-(--breakpoint-lg) py-4 mx-auto">
46-
<div class="absolute md:fixed right-8 md:left-4 top-3 w-10 h-10">
47-
<.link navigate={~p"/"}>
48-
<img src={~p"/images/logo.png"} height="40" widthz="40" alt="logo livebook" />
49-
</.link>
50-
</div>
5150
<div class="flex items-center pb-4 mb-2 space-x-4 border-b border-gray-200 pr-20 md:pr-0">
5251
<h1 class="text-3xl font-semibold text-gray-800">
5352
{@app.notebook_name}

lib/livebook_web/live/app_session_live.ex

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ defmodule LivebookWeb.AppSessionLive do
6060
|> assign(
6161
slug: slug,
6262
session: session,
63+
apps_banner: Livebook.Config.apps_banner(),
6364
page_title: get_page_title(data.notebook.name),
6465
client_id: client_id,
6566
data_view: data_to_view(data)
@@ -107,57 +108,58 @@ defmodule LivebookWeb.AppSessionLive do
107108

108109
def render(assigns) when assigns.app_authenticated? and assigns.app_authorized? do
109110
~H"""
111+
<.apps_banner value={@apps_banner} />
110112
<div class="h-full relative overflow-y-auto px-4 md:px-20" data-el-notebook>
113+
<div class="absolute right-4 md:left-4 md:right-auto top-3.5">
114+
<.menu id="app-menu" position="bottom-right" md_position="bottom-left">
115+
<:toggle>
116+
<button class="flex items-center text-gray-900">
117+
<img src={~p"/images/logo.png"} height="40" width="40" alt="logo livebook" />
118+
<.remix_icon icon="arrow-down-s-line" />
119+
</button>
120+
</:toggle>
121+
<.menu_item :if={@livebook_authorized?}>
122+
<.link navigate={~p"/"} role="menuitem">
123+
<.remix_icon icon="home-6-line" />
124+
<span>Home</span>
125+
</.link>
126+
</.menu_item>
127+
<.menu_item>
128+
<.link navigate={~p"/apps"} role="menuitem">
129+
<.remix_icon icon="layout-grid-fill" />
130+
<span>Apps</span>
131+
</.link>
132+
</.menu_item>
133+
<.menu_item :if={@data_view.multi_session}>
134+
<.link navigate={~p"/apps/#{@data_view.slug}"} role="menuitem">
135+
<.remix_icon icon="play-list-add-line" />
136+
<span>Sessions</span>
137+
</.link>
138+
</.menu_item>
139+
<.menu_item :if={@data_view.show_source}>
140+
<.link
141+
patch={~p"/apps/#{@data_view.slug}/sessions/#{@session.id}/source"}
142+
role="menuitem"
143+
>
144+
<.remix_icon icon="code-line" />
145+
<span>View source</span>
146+
</.link>
147+
</.menu_item>
148+
<.menu_item :if={@livebook_authorized?}>
149+
<.link patch={~p"/sessions/#{@session.id}"} role="menuitem">
150+
<.remix_icon icon="terminal-line" />
151+
<span>Debug</span>
152+
</.link>
153+
</.menu_item>
154+
<.menu_item :if={Livebook.Config.logout_enabled?() and @current_user.email != nil}>
155+
<button phx-click="logout" role="menuitem">
156+
<.remix_icon icon="logout-box-line" />
157+
<span>Logout</span>
158+
</button>
159+
</.menu_item>
160+
</.menu>
161+
</div>
111162
<div class="w-full max-w-(--breakpoint-lg) py-4 mx-auto" data-el-notebook-content>
112-
<div class="absolute md:fixed right-4 md:left-4 md:right-auto top-3">
113-
<.menu id="app-menu" position="bottom-right" md_position="bottom-left">
114-
<:toggle>
115-
<button class="flex items-center text-gray-900">
116-
<img src={~p"/images/logo.png"} height="40" width="40" alt="logo livebook" />
117-
<.remix_icon icon="arrow-down-s-line" />
118-
</button>
119-
</:toggle>
120-
<.menu_item :if={@livebook_authorized?}>
121-
<.link navigate={~p"/"} role="menuitem">
122-
<.remix_icon icon="home-6-line" />
123-
<span>Home</span>
124-
</.link>
125-
</.menu_item>
126-
<.menu_item>
127-
<.link navigate={~p"/apps"} role="menuitem">
128-
<.remix_icon icon="layout-grid-fill" />
129-
<span>Apps</span>
130-
</.link>
131-
</.menu_item>
132-
<.menu_item :if={@data_view.multi_session}>
133-
<.link navigate={~p"/apps/#{@data_view.slug}"} role="menuitem">
134-
<.remix_icon icon="play-list-add-line" />
135-
<span>Sessions</span>
136-
</.link>
137-
</.menu_item>
138-
<.menu_item :if={@data_view.show_source}>
139-
<.link
140-
patch={~p"/apps/#{@data_view.slug}/sessions/#{@session.id}/source"}
141-
role="menuitem"
142-
>
143-
<.remix_icon icon="code-line" />
144-
<span>View source</span>
145-
</.link>
146-
</.menu_item>
147-
<.menu_item :if={@livebook_authorized?}>
148-
<.link patch={~p"/sessions/#{@session.id}"} role="menuitem">
149-
<.remix_icon icon="terminal-line" />
150-
<span>Debug</span>
151-
</.link>
152-
</.menu_item>
153-
<.menu_item :if={Livebook.Config.logout_enabled?() and @current_user.email != nil}>
154-
<button phx-click="logout" role="menuitem">
155-
<.remix_icon icon="logout-box-line" />
156-
<span>Logout</span>
157-
</button>
158-
</.menu_item>
159-
</.menu>
160-
</div>
161163
<div data-el-js-view-iframes phx-update="ignore" id="js-view-iframes"></div>
162164
<div class="flex items-center pb-4 mb-2 space-x-4 border-b border-gray-200 pr-20 md:pr-0">
163165
<h1 class="text-3xl font-semibold text-gray-800">

lib/livebook_web/live/apps_live.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule LivebookWeb.AppsLive do
22
use LivebookWeb, :live_view
33

4+
import LivebookWeb.AppComponents
5+
46
@app_folder_events [
57
:app_folder_created,
68
:app_folder_updated,
@@ -19,6 +21,7 @@ defmodule LivebookWeb.AppsLive do
1921
{:ok,
2022
socket
2123
|> assign(
24+
apps_banner: Livebook.Config.apps_banner(),
2225
search_term: "",
2326
selected_app_folder: "",
2427
apps: Livebook.Apps.list_authorized_apps(socket.assigns.current_user),
@@ -34,6 +37,7 @@ defmodule LivebookWeb.AppsLive do
3437
def render(assigns) do
3538
~H"""
3639
<div class="h-full flex flex-col overflow-y-auto bg-white">
40+
<.apps_banner value={@apps_banner} />
3741
<div class="border-b border-gray-200/70 bg-white/80 backdrop-blur-sm shadow-sm">
3842
<div class="max-w-6xl mx-auto px-10 py-3.5 flex items-center justify-between">
3943
<div class="w-10 h-10">

test/livebook/config_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ defmodule Livebook.ConfigTest do
7171
end
7272
end
7373

74+
test "apps_banner!/1 parses the apps banner" do
75+
refute Config.apps_banner!("TEST_APPS_BANNER")
76+
77+
with_env([TEST_APPS_BANNER: ""], fn ->
78+
refute Config.apps_banner!("TEST_APPS_BANNER")
79+
end)
80+
81+
with_env([TEST_APPS_BANNER: "MyAppsBanner"], fn ->
82+
assert Config.apps_banner!("TEST_APPS_BANNER") == "MyAppsBanner"
83+
end)
84+
end
85+
7486
defp with_env(env_vars, fun) do
7587
existing =
7688
Enum.map(env_vars, fn {env, _value} ->

0 commit comments

Comments
 (0)