Ask Your Question

Enhanced table

asked 2020-04-04 12:24:22 -0600

Cyrille gravatar image

updated 2020-04-10 13:17:27 -0600

slelievre gravatar image

This code creates a nice table

sage: rows = [[100, 2, 3], [4, 5, 60]]
sage: t = table(rows, header_row=['a', 'b', 'c'], header_column=['', 'aa', 'bb'])
sage: show(t)

but aa and bb are not in bold. How to obtain a bold face and some separators between lines and columns?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2020-04-04 15:29:11 -0600

dsejas gravatar image

Hello, @Cyrille!

Am I correct to assume you want this in SageCell output? If that is the case, you can obtain bold fonts specifying manually that option: instead of 'a' in header_column, you can write r'<b>aa</b>'. The "r" just before the first simple quote sign indicates that this is a "raw string", so it must be used AS IS written by you; in this case, we are using a little bit of HTML, using the <b> and </b> to indicate bold font. So, you have to do:

rows = [[100,2,3], [4,5,60]]
t = table(rows, header_row=['a', 'b','c'], header_column=['',r'<b>aa</b>', r'<b>bb</b>'])

image description

Now, I don't think this is exactly what you want. First of all, the aa and bb will appear to be blue. You can change that by using a little bit more of HTML:

rows = [[100,2,3], [4,5,60]]
t = table(rows, header_row=['a', 'b','c'], header_column=['',r'<b style="color:black">aa</b>', r'<b style="color:black">bb</b>'])

image description

Finally, I understand that you want a frame for your table. You have to use the frame=True option:

rows = [[100,2,3], [4,5,60]]
t = table(rows, header_row=['a', 'b','c'], header_column=['',r'<b style="color:black">aa</b>', r'<b style="color:black">bb</b>'], frame=True)

image description

Of course, you can adapt this technique to other forms of output. For example, if you were working on Sage through LaTeX (by means of SageTeX, for example), you could do the following, which is equivalent to what we just did:

rows = [[100,2,3], [4,5,60]]
t = table(rows, header_row=[r'\textbf{a}','\textbf{b}','\textbf{c}'], header_column=['',r'\textbf{aa}', r'\textbf{bb}'], frame=True)

However, do be careful, since the frame=True option already highlights the headers for the rows and columns by placing a double line separation, so perhaps using bold font is too much.

image description

One more thing: If you don't like the alignment of the columns, remember you can always use the align='center' option.

edit flag offensive delete link more


Thanks Nice answer

Cyrille gravatar imageCyrille ( 2020-04-04 20:57:18 -0600 )edit

I have just a little comment. On my computer frame=True doesn't works

Cyrille gravatar imageCyrille ( 2020-04-06 05:54:14 -0600 )edit

Hello, @Cyrille! It seems that frame=True doesn't work properly on Jupyter notebooks. I tested my code in the Sage terminal, SageCell and CoCalc, and it seems to work properly there, although the results are a little different in each environment.

I have posted this related question, so we can find a solution.

dsejas gravatar imagedsejas ( 2020-04-06 10:35:18 -0600 )edit

Hello, @Cyrille! There seems to be a problem (outdated code) in the table() command code. You can see the question I mentioned in my previous comment. There, you can also see @Juanjo's answer, which will help you to make tables as you expected to. Basically, in Jupyter, you have to put that code in one cell, press CTRL+ENTER, and then create your table in any cell following that one.

Although this is a temporary solution, you can also use that answer to further customize tables in Jupyter. I will try to send a patch to the Sage source code to improve tables a little bit, including this frame=True argument.

dsejas gravatar imagedsejas ( 2020-04-06 19:16:57 -0600 )edit

@dsejas. In fact, the cell of the form


given in the other question can be placed everywhere in the notebook. The important thing is to not delete its output or reevaluate this cell if output is globally cleared (for example, after choosing Kernel>Restart&Clear Output). This is the way I customize the layout of my notebooks.

Juanjo gravatar imageJuanjo ( 2020-04-06 20:29:35 -0600 )edit

answered 2020-04-10 14:34:38 -0600

slelievre gravatar image

One way to obtain tables that display nicely is to use the Pandas package.

import pandas as pd
pd.DataFrame(data={'a': [100, 4], 'b': [2, 5], 'c': [3, 60]}, index=['aa', 'bb'])

To use it in Sage, you first need to install it.

This can be done by running this in the terminal:

sage --pip install pandas
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2020-04-04 12:24:22 -0600

Seen: 167 times

Last updated: Apr 10