# Enhanced table

This code creates a nice table

sage: rows = [[100, 2, 3], [4, 5, 60]]
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 close merge delete

Sort by » oldest newest most voted

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]]
show(t)


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]]
show(t)


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]]
show(t)


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]]
show(t)


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.

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

more

( 2020-04-04 20:57:18 -0600 )edit

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

( 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.

( 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.

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

@dsejas. In fact, the cell of the form

%%html
<style>
...
</style>


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.

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

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

more

## Stats

Seen: 183 times

Last updated: Apr 10 '20