Skip to content

Conversation

@nilaybhatia
Copy link
Contributor

@nilaybhatia nilaybhatia commented Aug 30, 2020

  1. Closes Need a Variable class for intuitively using continuously updated values  #375
  2. Working example (will be updated as functionality is refined):
class VariableExample(Scene):
    def construct(self):
        var = 0.5
        on_screen_var = Variable(var, "var", num_decimal_places=3)

        # You can also change the colours for the label and value
        on_screen_var.label.set_color(RED)
        on_screen_var.value.set_color(GREEN)

        self.play(Write(on_screen_var))
        # The above line will just display the variable with
        # its initial value on the screen. If you also wish to
        # update it, you can do so by accessing the `tracker` attribute
        self.wait()
        var_tracker = on_screen_var.tracker
        var = 10.5
        self.play(var_tracker.set_value, var)
        self.wait()

        int_var = 0
        on_screen_int_var = Variable(int_var, "int_var", var_type=Integer).next_to(
            on_screen_var, DOWN
        )
        on_screen_int_var.label.set_color(RED)
        on_screen_int_var.value.set_color(GREEN)

        self.play(Write(on_screen_int_var))
        self.wait()
        var_tracker = on_screen_int_var.tracker
        var = 10.5
        self.play(var_tracker.set_value, var)
        self.wait()

        # If you wish to have a somewhat more complicated label for your
        # variable with subscripts, superscripts, etc. wrap your label
        # string with `$`
        subscript_label_var = 10
        on_screen_subscript_var = Variable(subscript_label_var, "${a}_{i}$").next_to(on_screen_int_var, DOWN)
        self.play(Write(on_screen_subscript_var))
        self.wait()

NOTE: Until #302 is fixed, please use --disable_caching to render the video.
VariableExample

List of Changes

  • Add the Variable class to the file ./manim/mobject/numbers.py
  • Add the VariableExample class to example_scenes/basic.py

Motivation

Discussed here: #375

Explanation for Changes

Using the Variable class really makes it easy to use continually updated values. See the example above.

Testing Status

I think tests aren't required because the class makes use of updaters and tex, which already have tests.

Further Comments

In the meantime, please feel free to suggest improvements or some more functionalities that can be offered.
TODO:

  • Allow a more refined class for the value, for e.g DecimalNumber (with number of decimal places) or an Integer.

Acknowledgement

@leotrs leotrs added help wanted We would appreciate help on this issue/PR new feature Enhancement specifically adding a new feature (feature request should be used for issues instead) labels Aug 30, 2020
@leotrs
Copy link
Contributor

leotrs commented Aug 30, 2020

Thanks @nilaybhatia , another awesome addition!

@nilaybhatia
Copy link
Contributor Author

nilaybhatia commented Aug 31, 2020

Variable now inherits from VMobject instead of VDict. Please take a fresh look at the updated example (code+gif) in the PR description. @leotrs @Aathish04

@leotrs
Copy link
Contributor

leotrs commented Sep 2, 2020

@nilaybhatia I believe this is ready for review, no? I'm unmarking as draft.

@leotrs leotrs marked this pull request as ready for review September 2, 2020 01:31
@nilaybhatia
Copy link
Contributor Author

nilaybhatia commented Sep 2, 2020

@leotrs Yes, pending the docs and tests. Ready now.

@leotrs leotrs requested a review from Aathish04 September 3, 2020 11:30
Copy link
Member

@Aathish04 Aathish04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One suggestion, and two nitpicks :)

nilaybhatia and others added 3 commits September 3, 2020 20:10
Co-authored-by: Aathish Sivasubrahmanian <[email protected]>
Co-authored-by: Aathish Sivasubrahmanian <[email protected]>
Co-authored-by: Aathish Sivasubrahmanian <[email protected]>
leotrs
leotrs previously approved these changes Sep 3, 2020
@Aathish04
Copy link
Member

@nilaybhatia Awesome! Could you just add a note in the changelog file mentioning the introduction of this object? I totally forgot to mention it in my first review !

@Aathish04 Aathish04 requested a review from leotrs September 3, 2020 15:27
@leotrs leotrs merged commit a153680 into ManimCommunity:master Sep 3, 2020
@leotrs
Copy link
Contributor

leotrs commented Sep 3, 2020

🎉 🎉 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

help wanted We would appreciate help on this issue/PR new feature Enhancement specifically adding a new feature (feature request should be used for issues instead)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Need a Variable class for intuitively using continuously updated values

3 participants