Coder Social home page Coder Social logo

Comments (11)

josevalim avatar josevalim commented on June 10, 2024

Hi @davydog187! This is an Elixir behaviour, you can't access variables from "outside" inside functions. But you should be able to pass them inside by using a module attribute:

ExUnit.start()

defmodule SportFeed.Test do
  use ExUnit.Case
  @feed Kino.Input.textarea("feed")

  setup do
    %{data: Kino.Input.read(@feed)}
  end

  test "it works", %{data: data} do
    
  end
end

ExUnit.run()

You can also read the data before you store it.

from kino.

davydog187 avatar davydog187 commented on June 10, 2024

Thanks @josevalim. That was an approach I had tried, but you will get the following error

** (ArgumentError) cannot inject attribute @feed into function/macro because cannot escape #Reference<0.2064788886.4111204353.192084>. The supported values are: lists, tuples, maps, atoms, numbers, bitstrings, PIDs and remote functions in the format &Mod.fun/arity
    (elixir 1.13.0) lib/kernel.ex:3509: Kernel.do_at/5
    (elixir 1.13.0) expanding macro: Kernel.@/1
    feed_parser.livemd#cell:9: SportFeed.Test.__ex_unit_setup_0/1

from kino.

jonatanklosko avatar jonatanklosko commented on June 10, 2024

@josevalim we could make the input reference a string, we already do that for the JS widgets, so it would be consistent (it's more of an id, but we use the word id for reevaluation safe id 🙈). WDYT?

from kino.

davydog187 avatar davydog187 commented on June 10, 2024

If that's a reasonable solution I can send a PR

from kino.

josevalim avatar josevalim commented on June 10, 2024

Sounds good to me! @jonatanklosko, how you want to generate the binary references?

from kino.

jonatanklosko avatar jonatanklosko commented on June 10, 2024

I'd go with the same way we generate ids in Livebook, which is:

:crypto.strong_rand_bytes(20) |> Base.encode32(case: :lower)

I'll send a PR shortly, since there is probably a number of typespecs/docs to adjust too.

from kino.

jonatanklosko avatar jonatanklosko commented on June 10, 2024

@davydog187 for the record here's a working example https://gist.github.com/jonatanklosko/2ce5865e361ff1a70e43b3dcc686c1fa :)

from kino.

davydog187 avatar davydog187 commented on June 10, 2024

great, thank you @jonatanklosko!

from kino.

BrooklinJazz avatar BrooklinJazz commented on June 10, 2024

Reading this over again (thank you a ton for doing this!)

I realized that @davydog187 and @jonatanklosko use a different methods for accessing the value from kino.

Dave put the Kino input in the module attribute.

defmodule SportFeed.Test do
  @feed Kino.Input.textarea("feed")
end

And Jonatan read the input in the module attribute.

feed = Kino.Input.textarea("feed")
defmodule SportFeed.Test do
  @feed Kino.Input.read(feed)
end

Wanted to clarify in case anyone tries to use the first option and it doesn't work.
I'm not sure if the second option did or did not work originally, but regardless thank you all!

from kino.

jonatanklosko avatar jonatanklosko commented on June 10, 2024

@BrooklinJazz actually it's more like

# Cell 1
feed = Kino.Input.textarea("feed")
# [ Feed input ]

# Cell 2
defmodule SportFeed.Test do
  @feed feed

  # Then Kino.Input.read(feed) in tests
end

The point is that we need to render the input. We could also do @feed Kino.Input.textarea("feed") |> Kino.render(), but that's less obvious.

In practice reading the value as you mentioned totally does the job, because re-running the tests involves redefining the module anyway :)

from kino.

BrooklinJazz avatar BrooklinJazz commented on June 10, 2024

@jonatanklosko Woah! @feed Kino.Input.textarea("feed") |> Kino.render() totally works. That's really cool. This also helps me render multiple inputs so that's awesome.

Huge thank you 🙏

from kino.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.