Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple table drawing functionality. #841

Closed
wants to merge 1 commit into from
Closed

Add simple table drawing functionality. #841

wants to merge 1 commit into from

Conversation

Aathish04
Copy link
Contributor

@Aathish04 Aathish04 commented Dec 27, 2019

Motivation

Tables are really useful for displaying data concisely, which is really useful when making animations on topics such as statistics and science. Currently, Manim can only generate Tables easily through LaTeX, which results in difficulty when generating tables programmatically.

Results Obtained

With this addition, tables can be made by calling Table.get_table(<dictionary>), where the main, required parameter is a dictionary with the Keys as the field names and Values per Key are the corresponding records.

from manimlib.imports import *
class Tables(Scene):
    def construct(self):
        tabledict={
            "Vowels":["\\alpha","\\epsilon","\\iota","\\omega","a","e","i","o"],
            "Consonants":["\\beta","\\gamma","\\delta","\\phi","\\eta","\\kappa","\\lambda","\\mu","\\nu","\\pi","\\chi","\\zeta"],
            "Words":["Must","add","proper","length","checking","for","use","with","LaTeX.","(No","wraparound","currently.)"],
        }
        table=Table.get_table(tabledict)
        table.scale(0.50)
        table.move_to((0,0,0))
        self.play(Write(table))
        self.wait(2)

The code above generates the following table:

Tables

Further customisations can be made by passing a line_color and text_color in the same function like so: Table.get_table(tabledict,line_color=GREEN,text_color=BLUE)
Both of these values default to WHITE.

Changes Made:

A single file named tables.py has been added to manimlib/mobjects. This file contains the entirety of the logic used.

manimlib.imports has been edited to import the aforementioned file.

Future plans for this feature include a wraparound for text and better length measuring of LaTeX strings to ensure proper table column widths, as well as better table element retrieval, since you can only retrieve elements by indexing the resultant VGroup() as of now.

EDIT: I'm closing this PR, since I feel this would work better as an addon to Manim rather than as a core feature. I added support for Text(), TexMobjects(), TextMobjects() and strings. Those interested can find it here. An example on how to use all its features are here.

@Aathish04 Aathish04 changed the title Added simple table drawing functionality. Add simple table drawing functionality. Dec 27, 2019
@Aathish04 Aathish04 closed this Jan 5, 2020
@rgsrepo
Copy link

rgsrepo commented Feb 12, 2020

Hi thanks for this great work. Can you update the table to add info in it after it has been created?

@Aathish04
Copy link
Contributor Author

Aathish04 commented Feb 12, 2020

Hi thanks for this great work. Can you update the table to add info in it after it has been created?

I'm still working on that. However, as a workaround, you can index the table like a list, since it's just a VGroup.

The way I've set it up now, the first few objects in the VGroup are the field names/headings.

After the field names, the next few objects are reserved for those in the first column (the leftmost one) and are ordered top to bottom.

The next few objects are the values in the column to the immediate right of it and so on, until all the records are in the VGroup.

The object present immediately after the records is the horizontal line. The objects present after that are the vertical separators.

Once you know which object to modify, you can ReplacementTransform() them with the new value you want them to be.

For example:

from manimlib.imports import *
from sanim.anim_tools.tables import *
class Tables(Scene):
    def construct(self):
        
        tabledict={
            TextMobject("TextMobject Input"):[TextMobject("Must"),TextMobject("add"),TextMobject("element"),TextMobject("retrieval.")],
            TexMobject("TexMobject Input"):[TexMobject(r"e^{\iota\pi}+1 = 0"),TexMobject(r"Tex: \alpha\theta\epsilon")],
            Text("Text input",font="Lucida Grande"):[Text("Text",font="sans-serif"),Text("is"),Text("Supported")],
            "Raw String Input":["Defaults","to","TextMobject."]

        }

        table=Table.get_table(tabledict,line_color=GREY,raw_string_color=BLUE)
        table.move_to((0,0,0))
        self.play(Write(table.scale(0.5)),run_time=2)
        #CHECK THIS OUT ↓
        newvalue=TextMobject("New Value").move_to(table[0]) #The first index is the first columns heading
        self.play(ReplacementTransform(table[0],newvalue))

        self.wait(1)

The code above gives this output:

TableExample

Adding info will be a bit more difficult. I'll get to that soon!

@rgsrepo
Copy link

rgsrepo commented Feb 12, 2020

Thanks it worked. The counting does not include empty table cells? It's a bit strange. Looking forward to the rest of your code!

@Aathish04
Copy link
Contributor Author

Aathish04 commented Feb 12, 2020 via email

@Aathish04
Copy link
Contributor Author

Aathish04 commented Feb 17, 2020

Thanks it worked. The counting does not include empty table cells? It's a bit strange. Looking forward to the rest of your code!

Hey! I managed to add basic Record addition and removal to the table.

You can get the most recent version of the script here: https://github.com/Aathish04/sanim/

I've had to make some major changes regarding how everything works.

Table() is now it's own type of Mobject, and not just a VGroup. You can still use it like a VGroup though.

You can use Table.add_record(record object,field_number) to add records.
You can use Table.remove_record(field_number,record_number) to remove the record_numbered record in a field.

See the example below:

from manimlib.imports import *
from sanim.anim_tools.tables import *
class Tables(Scene):
    def construct(self):
        
        tabledict={
            TextMobject("TextMobject Input"):[TextMobject("Must"),TextMobject("add"),TextMobject("element"),TextMobject("retrieval.")],
            TexMobject("TexMobject Input"):[TexMobject(r"e^{\iota\pi}+1 = 0"),TexMobject(r"Tex: \alpha\theta\epsilon")],
            "Raw String Input":["Defaults","to","TextMobject."],
            Text("Text input",font="Lucida Grande"):[Text("Text",font="Alys Script Bold"),Text("is",font="serif"),Text("Supported",font="serif")],
        }

        table=Table(tabledict=tabledict,line_color=GRAY,raw_string_color=BLUE)
        table.move_to((0,0,0))
        table.scale(0.5)
        
        self.play(Write(table),run_time=2)

        self.play(Write(table.add_record(record=TextMobject("Hello"),field_num=1)))
        
        self.play(Uncreate(table.remove_record(field_num=1,record_num=0)))
        
        self.wait(1)

Hope this helps! I'm still working on Field addition and removal. That is a lot harder than I thought ;)

@rgsrepo
Copy link

rgsrepo commented Feb 17, 2020

For the empty string, I had already tried and the cell was ignored in the numbering.

Thanks a lot for the new code, I'll use it asap and let you know if I have any feedback.

@Aathish04
Copy link
Contributor Author

Aathish04 commented Feb 17, 2020 via email

@aieml
Copy link

aieml commented Apr 3, 2020

image

I am getting this error. Any Suggestions?

@Aathish04
Copy link
Contributor Author

Aathish04 commented Apr 4, 2020 via email

@almccutc
Copy link

almccutc commented Jul 8, 2020

I'm having the error:

table=Table.get_table(tabledict)

NameError: name 'Table' is not defined

I put the file tables.py into the folder manimlib/mobjects, not sure what the issue is.

@TonyCrane
Copy link
Collaborator

I'm having the error:

table=Table.get_table(tabledict)

NameError: name 'Table' is not defined

I put the file tables.py into the folder manimlib/mobjects, not sure what the issue is.

Have you added from manimlib.mobjects.tables import * to manimlib/imports.py ?

@Aathish04
Copy link
Contributor Author

I'm having the error:

table=Table.get_table(tabledict)

NameError: name 'Table' is not defined

I put the file tables.py into the folder manimlib/mobjects, not sure what the issue is.
@almccutc

Like @tony031218 mentioned, also add from manimlib.mobjects.tables import * to manimlib/imports.py, so you can import it that way.

Also, Table.get_table(<dict>) has been deprecated, and you should use Table(<dict>) instead.

@Aathish04
Copy link
Contributor Author

FOR THE REFERENCE OF ANYONE WHO STUMBLES UPON THIS THREAD:
Please use https://github.com/Aathish04/sanim/blob/manim-3b1b/anim_tools/tables.py for the most recent, bug free version of this feature.

@zdarovakoresh
Copy link

Simple adding the latest version of tables.py doesn't work. But the previous version (95 lines) works good.

@Aathish04
Copy link
Contributor Author

Aathish04 commented Aug 28, 2020

Simple adding the latest version of tables.py doesn't work. But the previous version (95 lines) works good.

@zdarovakoresh

Can you please file an issue on the repository I linked stating what went wrong and the traceback/errors, if any?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants