Skip to content

Commit

Permalink
Use kinos to render results
Browse files Browse the repository at this point in the history
  • Loading branch information
mruoss committed Nov 3, 2023
1 parent ec285a4 commit 465e24e
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 34 deletions.
52 changes: 34 additions & 18 deletions dev.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,61 @@

```elixir
# Mix.install([{:kino_k8s, github: "mruoss/kino_k8s"}])
Mix.install([{:kino_k8s, path: "~/src/community/kino_k8s"}])
Mix.install([{:kino_k8s, path: "~/src/community/kino_k8s"}], force: true)
```

## Section

<!-- livebook:{"attrs":{"opts":{"context":"rancher-desktop","insecure_skip_tls_verify":true},"result_variable":"conn","source":"~/.kube/config","source_type":"file"},"chunks":null,"kind":"Elixir.KinoK8s.ConnectionCell","livebook_object":"smart_cell"} -->
<!-- livebook:{"attrs":{"opts":{"context":"kind-kino-k8s","insecure_skip_tls_verify":true},"result_variable":"conn","source":"~/.kube/config","source_type":"file"},"chunks":null,"kind":"Elixir.KinoK8s.ConnectionCell","livebook_object":"smart_cell"} -->

```elixir
{:ok, conn} =
K8s.Conn.from_file("~/.kube/config",
context: "rancher-desktop",
context: "kind-kino-k8s",
insecure_skip_tls_verify: true
)
```

```elixir

conn
```

<!-- livebook:{"attrs":{"connect_to":"exec","connect_tos":["exec","logs"],"connection":null,"connections":[],"container":null,"containers":null,"namespace":null,"namespaces":null,"pod":null,"pods":null},"chunks":null,"kind":"Elixir.KinoK8s.TerminalCell","livebook_object":"smart_cell"} -->
<!-- livebook:{"attrs":{"connect_to":"exec","connect_tos":["exec","logs"],"connection":{"conn_hash":99636626,"variable":"conn"},"connections":[{"conn_hash":99636626,"variable":"conn"}],"container":null,"containers":null,"namespace":"default","namespaces":["default","kube-node-lease","kube-public","kube-system","local-path-storage"],"pod":null,"pods":[]},"chunks":null,"kind":"Elixir.KinoK8s.TerminalCell","livebook_object":"smart_cell"} -->

```elixir

```

<!-- livebook:{"attrs":{"connection":{"conn_hash":37669473,"variable":"conn"},"connections":[{"conn_hash":37669473,"variable":"conn"}],"gvk":null,"namespace":null,"namespaces":null,"request_type":"get","request_types":["get","list","watch"],"resource":null,"resources":null,"result_type":null,"result_types":{"get":null,"list":["list","stream"],"watch":["stream"]},"result_variable":"result"},"chunks":null,"kind":"Elixir.KinoK8s.GetCell","livebook_object":"smart_cell"} -->
<!-- livebook:{"attrs":{"connection":{"conn_hash":99636626,"variable":"conn"},"connections":[{"conn_hash":99636626,"variable":"conn"}],"gvk":{"api_version":"v1","index":"v1configmaps","kind":"ConfigMap","name":"configmaps","namespaced":true},"namespace":"default","namespaces":["__ALL__","default","kube-node-lease","kube-public","kube-system","local-path-storage"],"request_type":"watch","request_types":["get","list","watch"],"resource":null,"resources":null,"result_type":"stream","result_types":{"get":null,"list":["list","stream"],"watch":["stream"]},"result_variable":"result"},"chunks":null,"kind":"Elixir.KinoK8s.GetCell","livebook_object":"smart_cell"} -->

```elixir
{:ok, result} =
K8s.Client.watch("v1", "configmaps", namespace: "default")
|> K8s.Client.put_conn(conn)
|> K8s.Client.stream()

Kino.Tree.new(result)
```

<!-- livebook:{"attrs":{"connect_to":"exec","connect_tos":["exec","logs"],"connection":null,"connections":[],"container":null,"containers":null,"namespace":null,"namespaces":null,"pod":null,"pods":null},"chunks":null,"kind":"Elixir.KinoK8s.TerminalCell","livebook_object":"smart_cell"} -->

```elixir

```

<!-- livebook:{"attrs":{"body":"kind: ConfigMap\napiVersion: v1\nmetadata:\n name: kino-k8s-cm\n namespace: default\ndata:\n key: default\n","connection":{"conn_hash":37669473,"variable":"conn"},"connections":[{"conn_hash":37669473,"variable":"conn"}],"method":"create","methods":["apply","create","update"],"result_variable":"applied_resource"},"chunks":null,"kind":"Elixir.KinoK8s.ApplyCell","livebook_object":"smart_cell"} -->

```elixir

<!-- livebook:{"attrs":{"body":"kind: ConfigMap\napiVersion: v1\nmetadata:\n name: kino-k8s-cm\n namespace: default\ndata:\n key: default\n","connection":{"conn_hash":99636626,"variable":"conn"},"connections":[{"conn_hash":99636626,"variable":"conn"}],"method":"create","methods":["apply","create","update"],"result_variable":"applied_resource"},"chunks":null,"kind":"Elixir.KinoK8s.ApplyCell","livebook_object":"smart_cell"} -->

````elixir
import YamlElixir.Sigil

{:ok, applied_resource} =
~y"""
kind: ConfigMap
apiVersion: v1
metadata:
name: kino-k8s-cm
namespace: default
data:
key: default
"""
|> K8s.Client.create()
|> K8s.Client.put_conn(conn)
|> K8s.Client.run()

Kino.Markdown.new("```yaml
#{Ymlr.document!(applied_resource)}
```
")
````
3 changes: 1 addition & 2 deletions lib/assets/apply_cell/main.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions lib/assets/connection_cell/main.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions lib/assets/get_cell/main.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion lib/assets/kino_terminal/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ async function o(n,r){await n.importCSS("https://cdn.jsdelivr.net/npm/[email protected].
<div class="k8s-xtermjs-container"></div>
</div>
`;let i=n.root.querySelector(".k8s-xtermjs-container");if(i){let t=new Terminal({convertEol:!0});t.onKey(({key:e})=>n.pushEvent("key",e)),t.open(i),n.handleEvent("print-terminal",e=>t.write(e)),n.handleEvent("dispose-terminal",()=>t.dispose()),t.write(r.buffer)}}export{o as init};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vLi4vYXNzZXRzL2tpbm9fdGVybWluYWwvbWFpbi50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsiaW1wb3J0IHsgS2lub0NvbnRleHQgfSBmcm9tICcuLi9raW5vJ1xuaW1wb3J0IHsgS2lub1Rlcm1pbmFsQXR0cnMgfSBmcm9tICcuL3R5cGVzJ1xuXG5leHBvcnQgYXN5bmMgZnVuY3Rpb24gaW5pdChjdHg6IEtpbm9Db250ZXh0LCBhdHRyczogS2lub1Rlcm1pb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MiOiAiQUFHQSxlQUFzQkEsRUFBS0MsRUFBa0JDLEVBQTBCLENBQ3JFLE1BQU1ELEVBQUksVUFDUiw0REFDRixFQUNBLE1BQU1BLEVBQUksU0FDUiwyREFDRixFQUVBQSxFQUFJLEtBQUssVUFBWTtBQUFBO0FBQUE7QUFBQTtBQUFBLE1BTXJCLElBQU1FLEVBQWdCRixFQUFJLEtBQUssY0FDN0Isd0JBQ0YsRUFFQSxHQUFJRSxFQUFlLENBQ2pCLElBQU1DLEVBQVksSUFBSSxTQUFTLENBQUUsV0FBWSxFQUFLLENBQUMsRUFDbkRBLEVBQVUsTUFBTSxDQUFDLENBQUUsSUFBQUMsQ0FBSSxJQUFNSixFQUFJLFVBQVUsTUFBT0ksQ0FBRyxDQUFDLEVBQ3RERCxFQUFVLEtBQUtELENBQWEsRUFDNUJGLEVBQUksWUFBWSxpQkFBbUJLLEdBQWlCRixFQUFVLE1BQU1FLENBQUksQ0FBQyxFQUN6RUwsRUFBSSxZQUFZLG1CQUFvQixJQUFNRyxFQUFVLFFBQVEsQ0FBQyxFQUM3REEsRUFBVSxNQUFNRixFQUFNLE1BQU0sQ0FDOUIsQ0FDRiIsCiAgIm5hbWVzIjogWyJpbml0IiwgImN0eCIsICJhdHRycyIsICJyb290Q29udGFpbmVyIiwgIms4c194dGVybSIsICJrZXkiLCAiZGF0YSJdCn0K
3 changes: 1 addition & 2 deletions lib/assets/terminal_cell/main.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions lib/kino_k8s.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
defmodule KinoK8s do
@moduledoc false
defimpl Kino.Render, for: K8s.Conn do
def to_livebook(conn) do
conn |> Kino.Tree.new() |> Kino.Render.to_livebook()
end
end
6 changes: 5 additions & 1 deletion lib/kino_k8s/apply_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ defmodule KinoK8s.ApplyCell do
|> K8s.Client.put_conn(unquote(quoted_var(connection.variable)))
|> K8s.Client.run()

unquote(quoted_var(result_variable)) |> Ymlr.document!() |> IO.puts()
Kino.Markdown.new("""
```yaml
#{Ymlr.document!(unquote(quoted_var(result_variable)))}
```
""")
end
|> Kino.SmartCell.quoted_to_string()
else
Expand Down
7 changes: 5 additions & 2 deletions lib/kino_k8s/connection_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ defmodule KinoK8s.ConnectionCell do
end

Kino.SmartCell.quoted_to_string(
quote do: {:ok, unquote(quoted_var(result_variable))} = unquote(expr)
quote do
{:ok, unquote(quoted_var(result_variable))} = unquote(expr)
unquote(quoted_var(result_variable))
end
)
else
""
Expand All @@ -67,7 +70,7 @@ defmodule KinoK8s.ConnectionCell do
@impl true
def scan_eval_result(_server, nil), do: :ok

def scan_eval_result(_server, {:ok, {:ok, conn}}) do
def scan_eval_result(_server, {:ok, conn}) do
ResourceGVKCache.init_cache(conn)
end

Expand Down
8 changes: 6 additions & 2 deletions lib/kino_k8s/get_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ defmodule KinoK8s.GetCell do
|> K8s.Client.put_conn(unquote(quoted_var(connection.variable)))
|> K8s.Client.run()

unquote(quoted_var(result_variable)) |> Ymlr.document!() |> IO.puts()
Kino.Markdown.new("""
```yaml
#{Ymlr.document!(unquote(quoted_var(result_variable)))}
```
""")
end
|> Kino.SmartCell.quoted_to_string()
else
Expand Down Expand Up @@ -240,7 +244,7 @@ defmodule KinoK8s.GetCell do
|> K8s.Client.put_conn(unquote(quoted_var(connection.variable)))
|> K8s.Client.unquote(run_method)()

unquote(quoted_var(result_variable))
Kino.Tree.new(unquote(quoted_var(result_variable)))
end
|> Kino.SmartCell.quoted_to_string()
else
Expand Down

0 comments on commit 465e24e

Please sign in to comment.