ipython notebook align table to the left of cell - jupyter-notebook

I have below ipython notebook code (markdown):
#### example table
|Name|Description|
|--|-------------------------------|
|Mary |She is a nice girl.|
|Jackie |He is a very naughty boy.|
The output looks like below:
How can I:
Left align the table of the cell, it's center by default now.
Right align the second col text.

Well, yes !
| Name | Description | age
| :- |-------------: | :-:
|Mary| She is a nice girl. | 20
| Jackie Junior | He is a very naughty boy. | 5
:--- or --- = left align
---: = right align
:---: = centered

Answer to the 1st question - left-align the table - create and run a code cell above the table markdown cell, with the following content:
%%html
<style>
table {float:left}
</style>

I would suggest to use this variant of the knuth's answer that doesn't affect the flow of the remaining elements around the table. Put this code inside a cell above the one containing the table:
%%html
<style>
table {margin-left: 0 !important;}
</style>

You can make a custom preference in Ipython.
Just make the following file
~/.ipython/profile_default/static/custom/custom.css
and add the following code.
table {float: left};
and You don't have to put the custom css in all ipython files.

I know many have already answered this, but personally, I found their answer lacks a little more description, and because of this many are not able to implement this.
Clarification
Also, I want to clear one thing that I am not giving the solution for aligning the values inside the table but for aligning the whole table rendering itself. And if you are looking solution for the table's cell alignment then you can consider the first answer itself
Solution
Here are the steps:
First create a Code cell (not markdown) just above your markdown cell where you want to show your table.
Then write the following in your Code cell inside it.
%%html
<style>
table {float:left}
</style>
Run your Code cell.
Now just create your table as normally you do, no need to add anything extra. And Voila! Your table should now render to the left.
For every notebook, you have to do this step for changing the table alignment. But if you don't want to do this, you can follow #Anderson answer. For ease, I am copying his answer here.
First you need to create a file named custom.css where you will put the following code
table {float: left};
Then you have to move this file to your ipython directories, it will be something like this
~/.ipython/profile_default/static/custom/custom.css
Hope it helped 😊

Yeah, I don't like that centered table either. Insert this at the top of your notebook after the imports section:
from IPython.core.display import HTML
table_css = 'table {align:left;display:block} '
HTML('<style>{}</style>'.format(table_css))
The IPython.core.display namespace allows you to imbed audio, local filelinks among others - as well as HTML within your notebook.

!important overrides the css of rendered _html
Use your styles with !important
<style> table td, table th, table tr {text-align:left !important;} </style>

Answering the fist question: When creating a table and assigning it the float: left attribute you end up having to add more CSS to resolve the text that surrounds the table. Place this in a code cell before your markdown cell
%%html
<style>
table {
display: inline-block
}
</style>
However, if you have a lot of CSS, might be best to put it in another file for the overall beauty of the document.
#jrjc answers the 2nd question perfectly ;)

If you look at the table in the inspector, you'll see that the cause of the issue is the fact that the margin-left and margin-right CSS properties on the table are set to auto, making it centered. You can make it left-aligned by doing something like this in your custom.css:
.rendered_html table {
margin-left: 0px;
}
That should only left-align those specific tables and not affect anything else.

Related

open html/css file with MS Word - element table - apply no text wrapping

I am looking to open an HTML/CSS file with MS Word. I have a table in the file and would like to set the table properties, text wrapping to NONE using CSS. I understand Word should be able to interpret the HTML/CSS.
I cannot find any CSS to apply NO text wrapping to an entire table. Can anyone help?
If you are writing your styles separately, you can define your css as such:
table th,
table td {
white-space: nowrap;
}
That is going to make ALL table header and regular cells not wrap, so be aware of that. If you are trying to make this change and you need to target a cell directly, you would have to make the above CSS more specific by adding class names to the cells you want to target. Or add the CSS inline to the cell: <td style="white-space:nowrap"></td>

What is the best approach to style code output in a Jupyter notebook?

I have a set of Jupyter notebooks I am developing using iPython as the underlying Kernel.
I want to style the output to make it stand out from the surrounding text, what is the best way to style my output?
You can place a custom.css file in the ~/.jupyter/custom folder which will affect the css.
To style the output, you'll need to use a selector like so:
.output_subarea * {
background-color: red;
}
Depending on what you want to style. The * matches all elements and the space between .output_subarea and * indicates we want to select the decendents of .output_subarea. !important indicates that we want to override any existing styles, you may need to use this. If it doesn't seem to be working, try adding on !important like so:
.output_subarea * {
background-color: red !important;
}
There's a couple of output selectors, you should play around with them until you find the one you want to use.
.output selects the whole output div, .output_area selects any output areas. Usually styling .output or .output_area will do similar things.
.output_subarea specifically only targets areas of actual outputs (i.e. it will exclude the prompt)
Play around with it, have a look at the DOM inspector using your browser to find the classes of the parts of the page you want to affec

Centering Headings in IPython notebook

I would like to center heading cells in IPython notebook.
I know it is possible to create centered headlines by writing HTML, but then I can't get a reference to the cell when using table of contents (nbtoc ext.)
Is there a way to write HTML headings with reference or heading cells with centered text?
Thanks!
If you want to center a heading without custom css, you can surround your text with center tags.
For example, if you wanted to make a centered h1 title cell:
# <center>Title</center>
Another option is to use:
<h3 align="center">This is a centered header</h3>
as shown here.
This could be done using a custom css; see this question for more details on setting up the profile and the appropriate files.
An example custom css can be found in Barba's 12 Steps to Navier Stokes course although the actual file may contain more detail than you need. The actual ipython notebooks there directly load the css, rather than using a profile method, which also works. To center the headings, just modify the css file to
h1 {
...
text-align: center;
}
as usual.

Can I Add Custom Formatting Markup to MediaWiki?

Is it possible to add custom formatting markup to MediaWiki?
Say, for example, I have a div style I use quite often and I'd like to make markup to apply it more quickly than using <div id="frequentlyusedstyle">Title</div> -- like surrounding the text with ##Title## instead of typing out the div id. Is that possible?
(I realize that there is already heading markup; that's just an example. Thank you in advance!)
Just create a new page named "Template:name", where 'name' is whatever you want to name it, with the following text (as per your example):
< div id="frequentlyusedstyle">{{{1|}}}< /div>
(minus the extra spaces, since I don't know how to keep html from
parsing here.)
You would then use it by adding {{template name|Title}} to an article, and it will invoke the style.
You will need to have a style defined in MediaWiki:Common.css or similar, in order to style that div, such as:
#frequentlyusedstyle {
color: red;
}
Hope that helps.

Restructured Text (Sphinx) Image in Heading?

Is it possible to place an image inside of a heading using Restructured Text?
Something like:
Introduction .. image:: path/to/img.png
---------------------------------------
This renders as text, i.e. the image syntax is not parsed. I have not seen any examples of this leading me to believe it might not be possible, but perhaps someone has a work-around.
I intend to output to HTML (so a modification to the derived CSS is possible, though I would prefer to alter the RST source. This is because I also intend to output to pdf (latex).
I think that a better result could be achieved using aliases (i.e. substitutions).
Here an extract from the documentation that can be helpful:
The |biohazard| symbol must be used on containers used to
dispose of medical waste.
.. |biohazard| image:: biohazard.png
I hope this helps
This can be achieved by using a substitution in the header:
Header Text |foo|
=================
.. |foo| image:: path/to/img.png
Here foo is just an example substitution text. It can be anything, but should not start or end with a whitespace.
The abstract substitution syntax is as follows:
+-------+-----------------------------------------------------+
| ".. " | "|" substitution text "| " directive type "::" data |
+-------+ directive block |
| |
+-----------------------------------------------------+
As we would like to insert an inline image, we should choose the image as the directive type.
With HTML and CSS there are a number of ways to add an image to an element which yield different results. A couple that I can think of are:
Use an image in place of any text. Here we keep the text, but send it off screen, so it is still accessible.
h1 {
background: url(images/image.jpg) no-repeat top center;
display: block;
text-indent: 100%;
white-space: nowrap;
overflow: hidden;
width: XXpx;
height: XXpx;
}
Place an image to the left of the text (or could easily be to the right)
h1 {
background: url(images/image.jpg) no-repeat top left;
padding-left: XXpx;
}
Use a background image, behind any text
h1 {
background:url(images/image.jpg) no-repeat top center;
}
More specific CSS selectors can be used to target only certain headers.
You can include custom CSS easily within reStructuredText documents using the raw directive, like so:
.. raw:: html
<style>
<!-- One of the CSS styles above. -->
</style>
Alternatively, you can include custom CSS stylesheets from the command line using the --stylesheet option of rst2html.py.
In terms of achieving the same with PDF output, I will steal part of another one of my answers:
Obviously the above targets HTML output. However, I have not used rst2pdf, so can't comment on how the above needs to be modified to work with this program. Hopefully someone else will provide an answer to this. As far as I know, rst2pdf does support a cascading stylesheet mechanism, so it should be straightforward (for someone who knows rst2pdf style sheet syntax) to add an additional .. raw:: pdf role and to modify the above list styles.

Resources