Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Styling of HTML in IPython Notebook output

I am attempting to style the html table output for an IPython output cell with an external css file. I would like help understanding how to do this and have created a couple of test cases for exploration. Neither inline or external styling behave as I expect -

External which I am hoping to do:

htmlstr = "<html><head><link rel='stylesheet' type=\"text/css\" href=\"local.css\"></head><body>TEST BODY</body></html>"
HTML(htmlstr)

The file does not seem to be read. I have tried different paths and moving the file around; but, it does not seem to be recognized.


Internal Styling:

htmlstr = "<html><head><style>body {background-color:yellow;}</style></head><body>TEST BODY</body></html>"
HTML(htmlstr)

Execution of this in IPython changes the background of IPython itself. That is the background changes to yellow for all of IPython and the input cells remain white. Which is pretty cool; but, I want to style the specific output. And again, I want to store the CSS in an external file. Can someone help me understand the behaviour?

IPython is great about providing many possibilities and it is possible that there is a better path for my need.

like image 362
tnt Avatar asked Nov 21 '13 15:11

tnt


People also ask

Can you use CSS in Jupyter notebook?

css in the directory ~/. jupyter/custom. If you have it, you can add some CSS code to it. But, if you have not had it, you need to create the file in the mentioned directory.

What does %% do in Jupyter notebook?

Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.

Does HTML work in Jupyter notebook?

Notebooks may be exported to a range of static formats, including HTML (for example, for blog posts), reStructuredText, LaTeX, PDF, and slide shows, via the nbconvert command. Furthermore, any . ipynb notebook document available from a public URL can be shared via the Jupyter Notebook Viewer <nbviewer>.


1 Answers

You could simply use more specific CSS properties! E.g. in a markdown cell (or custom.css with is the default external file for styling the notebook and its content)

<style>
th {
background-color:#55FF33;
}
td {
background-color:#00FFFF;
}
</style>

with the following code

from IPython.display import HTML
table = "<table><tr><th>bar</th><th>bar</th></tr><tr><td>foo</td><td>foo</td></tr></table>"
HTML(table)

gives
enter image description here

like image 195
Jakob Avatar answered Oct 05 '22 11:10

Jakob