Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

kino

stage2/kino.livemd

kino

Mix.install([
  {:kino, "~> 0.10.0"},
  {:req, "~> 0.4.5"}
])

Section

frame = Kino.Frame.new()
inputs = [name: Kino.Input.text("Name"), message: Kino.Input.text("Message")]
form = Kino.Control.form(inputs, submit: "Send", reset_on_submit: [:message])
Kino.listen(form, fn %{data: %{name: name, message: message}, origin: origin} ->
  if name != "" and message != "" do
    content = Kino.Markdown.new("**#{name}**: #{message}")
    Kino.Frame.append(frame, content)
  else
    content = Kino.Markdown.new("_ERROR! You need a name and message to submit..._")
    Kino.Frame.append(frame, content, to: origin)
  end
end)
frame2 = Kino.Frame.new()
inputs2 = [te: Kino.Input.text("Anata")]
form2 = Kino.Control.form(inputs2, submit: "Send", reset_on_submit: [:te])
defmodule Janken do
  def hand(c) do
    cond do
      c == 0 ->
        "グー"

      c == 1 ->
        "チョキ"

      c == 2 ->
        "パー"

      :else ->
        "うそ"
    end
  end

  def judge(a, b) do
    cond do
      a == b ->
        "あいこ"

      a == 0 && b == 1 ->
        "あなたの勝ち"

      a == 1 && b == 2 ->
        "あなたの勝ち"

      a == 2 && b == 0 ->
        "あなたの勝ち"

      a == 0 && b == 2 ->
        "あなたの負け"

      a == 1 && b == 0 ->
        "あなたの負け"

      a == 2 && b == 1 ->
        "あなたの負け"
    end
  end

  def pon(a) do
    res = ""

    {a, _} =
      a
      |> Integer.parse()

    res = res <> hand(a) <> "と"

    b =
      0..2
      |> Enum.random()

    res = res <> hand(b) <> "で、"
    res = res <> judge(a, b)
    res
  end
end

Kino.listen(form2, fn %{data: %{te: te}, origin: origin} ->
  res = Janken.pon(te)
  content = Kino.Markdown.new(res)
  Kino.Frame.append(frame2, content)
end)
button0 = Kino.Control.button("gu")
button1 = Kino.Control.button("choki")
button2 = Kino.Control.button("par")
Kino.Control.subscribe(button0, :gu)
Kino.Control.subscribe(button1, :choki)
Kino.Control.subscribe(button2, :par)
widget = Kino.Frame.new()
loop = fn f ->
  receive do
    {:gu, _} ->
      res = Janken.pon("0")
      Kino.Frame.append(widget, res)

    {:choki, _} ->
      res = Janken.pon("1")
      Kino.Frame.append(widget, res)

    {:par, _} ->
      res = Janken.pon("2")
      Kino.Frame.append(widget, res)

    _ ->
      :ok
  end
end

loop.(loo)
user1 = %{name: "alice", age: 15}
user2 = %{name: "bob", age: 19}
user3 = %{name: "carol", age: 23}

users = [user1, user2, user3]

users
|> Kino.DataTable.new()
%Req.Response{status: 200, body: body} =
  Req.get!("https://qiita.com/api/v2/users/ohisama@github/items")

# |> Kino.Tree.new()
# |> Map.get("body")

# |> IO.inspect()
body
|> Kino.DataTable.new()