I just upgraded to IPython 1.0 and the new notebook interface performs really well. On my screen it now has a default width of about 50% of the page which improves readability. However, i often work with long timeseries which i prefer to display as wide as possible.
Very wide pictures will only extend further to the right. Is there a way to display output wider then the default to expand in a centered matter?
The attached picture shows a normal inline plot in the first cell, which is less wide then the default notebook width. The second plot is wider and expands to the right. This leaves about the left quarter of my screen unused.
What I do is the following. This might help, but it also creates a beautifully rendered notebook.
Have a look at this online book about Bayesian Statistics. This is really nice.
They load a custom CSS at the end of the notebook using this code:
from IPython.core.display import HTML
def css_styling():
styles = open("custom.css", "r").read() #or edit path to custom.css
return HTML(styles)
css_styling()
You can download this custom CSS from the books github repo HERE: Drop it in the notebook folder and call the code above
Also note their nice matplotlibrc file. You can change the look and feel of the plots calling this code. Call this early in the notebook and all matplotlib plots will look pretty cool.
import json
s = json.load( open("bmh_matplotlibrc.json") ) #edit path to json file
matplotlib.rcParams.update(s)
Download the JSON file HERE from GITHUB
In short you have two options. Both add CSS styling to the notebook to achieve the effect:
Global Jupyter CSS (affects all notebooks)
Edit or create ~/.jupyter/custom/custom.css and add:
.output_png {
display: table-cell;
text-align: center;
vertical-align: middle;
}
Modify the style of the current notebook by executing a code cell with:
from IPython.core.display import HTML
HTML("""
<style>
.output_png {
display: table-cell;
text-align: center;
vertical-align: middle;
}
</style>
""")
Note that in the first case there is no <style></style>.
I prefer the second option, specially if I'm going to share the notebook (since its self-contained).
The answer given by #tooblippe, although changes the style of the notebook, does not address the question. However, using the first part of that solution, you would be able to achieve the centering of the plots. This solution works for ipython==3.1.0. For higher versions that include Jupyter, you would want to edit the styling of Jupyter instead.
So, the solution would be:
You can either modify the above css file or add this line to the styling of your ipython environment in the ipython folder:
.output_png {
display: table-cell;
text-align: center;
vertical-align: middle;
}
vertical-align:middle did not work for me. Also, in the latest Jupyter (maybe others, but at least) there is a class called .prompt that takes up space only on the left of the page. I was able to work around both using this:
.output_png {
display: table-cell;
text-align: center;
margin:auto;
}
.prompt
display:none;
}
Just know that setting display:none on .prompt will cause all of the in[] and out[] displays to disappear and know that's what you want if you're going to do it. Hope that was helpful.
All of the answers posted (at the time of writing) don't appear to work for Jupyter Lab v3.
Instead of changing the CSS for output_png, I've found that changing it for jp-RenderedImage does what I want. If this changes or is different for you for any reason, just inspect the element and choose the relevant class to modify.
The Plots in the Jupyter Notebook can be Centered by Using HTML CSS Code Snippet.
The Code for aligning the plots at center is shown below, by using this Code Snippet all the plots can be centered in the Jupyter Notebook.
from IPython.core.display import HTML as Center
Center(""" <style>
.output_png {
display: table-cell;
text-align: center;
vertical-align: middle;
}
</style> """)
Make Sure you run this Code Snippet in Code Cell, not in Markdown Cell.
Related
I feel like this could be a possible issue with how CodePen interprets things. I am new to CSS, so, would appreciate any explanations.
I have code running on CodePen. https://codepen.io/jay-pancodu/pen/bGjdbry
The output looks like this.
Now, that code, I also run locally. that code is available here. https://github.com/Jay-study-nildana/StackOverDoubtsWeb/tree/main/CalculatorwithBootstrap
When this is run locally, I get an output like this.
All I did was create a new Pen, copy pasted the code from the local files to the online codepen editor.
Why would this happen? is this normal CodePen behavior that I should simply anticipate?
Further, The original code is available here, https://codepen.io/giana/pen/GJMBEv. That looks just fine, and oddly enough, when I copy paste this codepen code to run locally, the layout gets screw up again.
So, I am just confused. And, I am new to CSS.
Adding some lines of code, because, the editor won't let me post without some lines of code.Please ignore this below code snippet.
body {
color: #181ce9;
font: 300 18px/1.6 "Source Sans Pro", sans-serif;
margin: 0;
padding: 5em 0 2em;
text-align: center;
}
I find that the normalize.css is the main reason why your CodePen won't work. Take note that CSS will make the last line for the main style. In CodePen CSS will always take the style on the top. And this normalize CSS is under the CSS Codepen style, different from your local that the normalize CSS is on top of your style.css
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
I need to render some html tables in Jupyter Notebook and for this I have defined my own css styles. I want it to be reusable on any PC.
For now I need to run code like this in one of Jupyter cells:
%%html
<style>
.output_wrapper, .output {
height:auto !important;
max-height: none;
}
.output_scroll {
box-shadow:none !important;
webkit-box-shadow:none !important;
}
.package_header {
width: 100%;
background-color: #0e2b59;
color: #ffffff;
font-size: 14px;
text-align: center;
padding-top: 8px;
padding-right: 8px;
padding-bottom: 8px;
padding-left: 8px;
}
.placeholder {
width: 100%;
background-color: #ffffff;
height: 6px;
}
.passed_test_table {
display: table;
width: 100%;
background-color: #ebffd3;
border-spacing: 5px;
}
# (...) rest of css classes omitted
</style>
Yet I don't want to store this style inside Jupyter Notebook but in other file like my_default_style.css and load it somehow so it doesn't take too much space in Notebook making it less readable.
Is it possible to load .css style from some local file instead of running it in Jupyter Notebook cell directly?
If you are okay with the styling applying to all notebooks you make, then you can store your css in ~/.jupyter/custom/custom.css and this will be automatically loaded and will override any default styles.
An alternative is to do something like this:
from IPython.core.display import HTML
import urllib2
HTML(urllib2.urlopen('[url to your css file here').read())
(from http://moderndata.plot.ly/custom-styling-for-ipython-notebooks-with-3-lines-of-code/)
Where you store your css file somewhere (e.g. on Github) and download it and apply it when you run the notebook.
If you want it to be completely external, use custom.css, but this will apply to every notebook. If you need it to only apply to a single notebook, then you'll have to specify it within the notebook, but there are less verbose ways of doing it (like above).
See configuration docs
If you are using Jupyter with Python 3 then there is no urllib2 (it was merged into urllib) and the answers above will give you the error ModuleNotFoundError: No module named 'urllib2'. You may want something like this instead:
from IPython.display import HTML
from urllib.request import urlopen
html = urlopen("[url to your css file here]")
HTML(html.read().decode('utf-8'))
Note that read() returns a bytes encoding, which you must decode if you want text.
If you are using an inline CSS style, you can also pass it as part of the HTML content, for example in a <style> block. Here is a simple example:
from IPython.display import HTML
css_str = '<style>.foo{color:#F00;}</style>'
html_str = '<div class="foo">bar</div>'
html = HTML(css_str + html_str)
display(html)
Output is the word "bar" in red.
This answer would probably better be a comment on the earlier answer by Davies and Narasimhan, but I lack the reputation to post a comment.
from IPython.core.display import HTML will work, and was the recommended way to access HTML display capability in earlier versions of IPython. But in recent versions, the public display API is in IPython.display (see its docstring). This module exposes __all__ of IPython.core.display, along with __all__ of IPython.lib.display. Notably, the current IPython docs (The IPython API) don't publicly document IPython.core.display separately. For the current IPython.display docs, see: Module: display — IPython documentation.
So the recommended solution as of IPython 5 I believe would be:
from IPython.display import HTML
import urllib2
HTML(urllib2.urlopen('[url to your css file here').read())
How can I add CSS to github's markdown language?
I've been able to do so by using the style property inside html tags, like:
<p style="text-align: center;">This is some random text</p>
But if I move the css to the beginning, like:
<style>
p {
text-align: center;
}
</style>
<p>This is some random text</p>
Github doesn't recognize it, and just writes to the screen the css code.
I'm using Atom, and the package Markdown Preview actually recognizes this correctly, even though on the remote repository it shows wrong. And so does the Google Chrome extension Markdown Preview Plus.
Is there a way to do this? Writing css within html tags just feels plain wrong.
After GitHub converts Markdown to HTML,
The HTML is sanitized, aggressively removing things that could harm you and your kin—such as script tags, inline-styles, and class or id attributes. See the sanitization filter for the full whitelist.
style tags are not included in GitHub's whitelist, so they are removed. I'm actually surprised that inline style attributes work; they don't seem to be included in the whitelist, either, and are explicitly mentioned in the previous paragraph.
In any case, GitHub does not permit arbitrary HTML to be included in Markdown.
Here is how you can accomplish what you're looking for. As the other answer states, Github doesn't support this syntax, but if you pop this Markdown into another preview tool you'll see that the bullets are removed from this list.
|Signal|Description|
|---|---|
|DOP|Horizontal Dilution of precision|
|FIX|GPS Fix Quality indicator: <ul style="list-style-type:none;"><li>0 - fix not available</li><li>1 - GPS fix</li></ul>|
Signal
Description
DOP
Horizontal Dilution of precision
FIX
GPS Fix Quality indicator: 0 - fix not available1 - GPS fix
You can trivially override what CSS Github uses by supplying it with your own style.css file, nested as ./assets/css/style.css (which is stylesheet URL that gets pointed to in the HTML source code that Github build off of your markdown).
Note that if you want to just "add" any CSS, you'll want to copy Github's CSS first, so you can create a file with the same content after which you place your own rules. You can find this on any view-source:https://username.github.io/repo-name/assets/css/style.css with the obvious replacements for username and repo-name.
E.g.
/* CSS as copied from github's own stylesheet here, which is all one line anyway */
...
/* And then your own CSS */
/* remove the repo name as some kind of weird super-title */
h1:first-child { display: none }
/* and better emphasise the _real_ title */
h1:nth-child(2) { font-size: 3em; }
/* let's also give images a subtle border */
img { border: 1px solid #DDD; }
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.
I am looking for a ressource where I can download CSS styles suitable for Rstudio/knitr markdown output?
The default look of the default CSS-style is fine, but I would like to find a CSS style where the content is positioned in the middle of the screen.
something like this (ignore content, colors, sidebar etc):
http://www.barackobama.com/news/
not like this (which is similar to the default):
http://stat.ethz.ch/R-manual/R-patched/library/stats/html/Normal.html
I don't really know CSS so I can't do it myself. I have tried to change the margin in the default CSS style from 0px to 200 px:
body, td {
font-family: sans-serif;
background-color: white;
font-size: 16px;
margin: 200px;
}
The problem with this "solution" is that it only works when the browser window is maximized, and pdf printed from the browser are too narrow also.
edit: This is good:
https://gist.github.com/andyferra/2554919
edit2: The preview version of Rstudio ( RStudio 0.98.932 - Windows XP/Vista/7/8) has a nice default CSS. Get it here: http://www.rstudio.com/products/rstudio/download/preview/
edit3: The newest version of Rstudio now includes some very nice CSS-styles to choose from :) http://www.rstudio.com/products/rstudio/download/
Not just a CSS resource, but you can take a look at the knitrBootstrap project, which provides a way to convert Rmarkdown to HTML styled with the bootstrap framework, including a CSS style chooser and some fancy javascript add-ons :
https://github.com/jimhester/knitrBootstrap