-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_live_example.ex
More file actions
284 lines (246 loc) · 8.13 KB
/
Copy pathcustom_live_example.ex
File metadata and controls
284 lines (246 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
defmodule Example.CustomLive do
@moduledoc """
Examples of using Crucible UI components in custom LiveViews.
These examples show how to integrate Crucible UI components into your
own Phoenix LiveViews for consistent styling and functionality.
"""
# Example 1: Custom dashboard with stat cards
defmodule DashboardExample do
use Phoenix.LiveView
import Crucible.UI.Components
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:experiment_count, 42)
|> assign(:running_count, 5)
|> assign(:completed_count, 37)
|> assign(:avg_accuracy, 0.89)}
end
def render(assigns) do
~H"""
<div class="space-y-8">
<.header>
ML Research Dashboard
<:subtitle>Real-time experiment tracking and analytics</:subtitle>
<:actions>
<.button phx-click="sync">Sync Data</.button>
<.button phx-click="export">Export Report</.button>
</:actions>
</.header>
<div class="grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-4">
<.stat_card
icon="hero-beaker"
label="Total Experiments"
value={@experiment_count}
icon_class="text-gray-400"
/>
<.stat_card
icon="hero-play"
label="Running"
value={@running_count}
icon_class="text-green-400"
/>
<.stat_card
icon="hero-check-circle"
label="Completed"
value={@completed_count}
icon_class="text-blue-400"
/>
<.stat_card
icon="hero-chart-bar"
label="Avg Accuracy"
value={"#{round(@avg_accuracy * 100)}%"}
icon_class="text-emerald-400"
/>
</div>
</div>
"""
end
end
# Example 2: Experiment list with table
defmodule ExperimentListExample do
use Phoenix.LiveView
import Crucible.UI.Components
alias Phoenix.LiveView.JS
def mount(_params, _session, socket) do
experiments = [
%{id: 1, name: "Baseline Model", status: "completed", accuracy: 0.85},
%{id: 2, name: "Fine-tuned v1", status: "running", accuracy: 0.91},
%{id: 3, name: "Fine-tuned v2", status: "pending", accuracy: nil}
]
{:ok, stream(socket, :experiments, experiments)}
end
def render(assigns) do
~H"""
<div class="space-y-6">
<.header>
My Experiments
<:actions>
<.link navigate="/experiments/new">
<.button>New Experiment</.button>
</.link>
</:actions>
</.header>
<.table
id="experiments"
rows={@streams.experiments}
row_click={fn {_id, exp} -> JS.navigate("/experiments/#{exp.id}") end}
>
<:col :let={{_id, exp}} label="Name">
<%= exp.name %>
</:col>
<:col :let={{_id, exp}} label="Status">
<.status_badge status={exp.status} />
</:col>
<:col :let={{_id, exp}} label="Accuracy">
<%= if exp.accuracy, do: "#{round(exp.accuracy * 100)}%", else: "N/A" %>
</:col>
<:action :let={{_id, exp}}>
<.link navigate={"/experiments/#{exp.id}"}>View</.link>
</:action>
<:action :let={{id, exp}}>
<.link
phx-click={JS.push("delete", value: %{id: exp.id}) |> JS.hide(to: "##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
</.table>
</div>
"""
end
end
# Example 3: Experiment details with actions
defmodule ExperimentDetailsExample do
use Phoenix.LiveView
import Crucible.UI.Components
def mount(%{"id" => id}, _session, socket) do
experiment = %{
id: id,
name: "Baseline Model",
description: "Initial baseline experiment with default hyperparameters",
status: "running",
started_at: ~U[2025-12-06 10:00:00Z],
completed_at: nil,
config: %{
model: "llama-3.1-8b",
epochs: 3,
learning_rate: 0.0001
}
}
{:ok, assign(socket, :experiment, experiment)}
end
def render(assigns) do
~H"""
<div class="space-y-6">
<.header>
<%= @experiment.name %>
<:subtitle><%= @experiment.description %></:subtitle>
<:actions>
<.button phx-click="pause">Pause</.button>
<.button phx-click="stop">Stop</.button>
</:actions>
</.header>
<.list>
<:item title="Status">
<.status_badge status={@experiment.status} />
</:item>
<:item title="Started">
<%= Calendar.strftime(@experiment.started_at, "%Y-%m-%d %H:%M:%S") %>
</:item>
<:item title="Completed">
<%= @experiment.completed_at || "In progress" %>
</:item>
<:item title="Configuration">
<pre class="text-xs bg-gray-50 p-2 rounded"><%= Jason.encode!(@experiment.config, pretty: true) %></pre>
</:item>
</.list>
<.back navigate="/experiments">Back to experiments</.back>
</div>
"""
end
end
# Example 4: Modal usage
defmodule ModalExample do
use Phoenix.LiveView
import Crucible.UI.Components
alias Phoenix.LiveView.JS
def mount(_params, _session, socket) do
{:ok, assign(socket, :show_modal, false)}
end
def handle_event("open_modal", _, socket) do
{:noreply, assign(socket, :show_modal, true)}
end
def handle_event("close_modal", _, socket) do
{:noreply, assign(socket, :show_modal, false)}
end
def render(assigns) do
~H"""
<div>
<.button phx-click="open_modal">Open Modal</.button>
<.modal id="confirm-modal" show={@show_modal} on_cancel={JS.push("close_modal")}>
<div class="space-y-4">
<h2 class="text-xl font-semibold">Confirm Action</h2>
<p class="text-gray-600">
Are you sure you want to delete this experiment?
This action cannot be undone.
</p>
<div class="flex gap-4 justify-end">
<.button phx-click="close_modal">Cancel</.button>
<.button phx-click="confirm_delete" class="bg-red-600 hover:bg-red-700">
Delete
</.button>
</div>
</div>
</.modal>
</div>
"""
end
end
# Example 5: Combining multiple components
defmodule CombinedExample do
use Phoenix.LiveView
import Crucible.UI.Components
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:total_runs, 156)
|> assign(:active_runs, 12)
|> assign(:recent_runs, [
%{id: 1, name: "Run #101", status: "running"},
%{id: 2, name: "Run #102", status: "completed"},
%{id: 3, name: "Run #103", status: "failed"}
])}
end
def render(assigns) do
~H"""
<div class="space-y-8">
<.header>
Run Monitor
<:subtitle>Real-time monitoring of experiment runs</:subtitle>
</.header>
<div class="grid grid-cols-2 gap-4">
<.stat_card icon="hero-chart-bar" label="Total Runs" value={@total_runs} />
<.stat_card icon="hero-play" label="Active" value={@active_runs} icon_class="text-green-400" />
</div>
<div class="bg-white shadow rounded-lg p-6">
<h3 class="text-lg font-semibold mb-4">Recent Runs</h3>
<div class="space-y-3">
<%= for run <- @recent_runs do %>
<div class="flex justify-between items-center p-3 border rounded hover:bg-gray-50">
<div class="flex items-center gap-3">
<.icon name="hero-play" class="h-5 w-5 text-gray-400" />
<span class="font-medium"><%= run.name %></span>
</div>
<.status_badge status={run.status} />
</div>
<% end %>
</div>
</div>
<.back navigate="/">Back to dashboard</.back>
</div>
"""
end
end
end