When passing style argument along with a <span> starting block, I changed font-size and font-family and all of that in my Jupyter notebook's individual cells. Like so-
<span style="font-family:Verdana">Irrelavant text.</span>
OR
# Model Building <span style="font-size:12px">[Jump to Beginning](#top)</span>
It was working just fine until about a week or so ago, when all of the style effects in my notebook just disappeared. The code is obviously still there in the markdown cells but it just doesn't show the intended result. The same notebook still works and loads the style when ran on Kaggle or JupyterLab. Did Jupyter notebooks stop supporting it or did I change something unintentionally? What could be a workaround or maybe even a fix for the issue without changing the code?
It used to look like this (avoid the black theme, screenshot is from JupyterLab)-
And now it looks like this-
As you can see, the font-size:12px has no effect anymore. Same goes for font-family and the likes.
Edit: Notebook
Inline styles in Markdown cells were temporarily removed in a security fix addressing a different sanitization issue earlier last month and restored in just released v6.4.4. Please update your notebook installation for the old styling in Markdown to work again.
JupyterLab 3.2.2, 3.2.3 and 3.2.4 are also affected by a similar issue. It was already fixed in PR #11510 and the fix was released in JupyterLab v3.2.5 on 10th December 2021.
I don't think this is the answer you wanted, but it works. It is scalable, too. I usually use Python with Atom, XCode, or RMarkdown in RStudio, so I am not all that familiar with the ins and outs of Jupyter's interface.
First, I noticed that I could see the text rendered as expected when I went to print preview. However, I thought that that was pretty useless. What are you going to do? Go to print preview every time you write something? What's the purpose of an interactive notebook at that point?
I digress.
Okay, so what I found that worked... in no way is this an idea that is originally mine...
Custom CSS
Adding a custom CSS file, but not the 'change it all' thing that the Jupyter help files suggest...
Step 1)
Create a styles folder in the same directory as the ipynb file.
Step 2)
Within the styles folder, create a CSS file.
Step 3)
Within that CSS file, write the two tag styles and any others you desire.
Here's that code (pictures of code are annoying).
div.verdana {
font-family:verdana;
}
div.bigger {
font-size: 20pt;
}
Within the ipynb File
Step 4)
Next, add the div tags to your ipynb file in a Markdown cell:
<div class='verdana'>This should be Verdana font.</div>
The result after running the cell:
Step 5)
Do the same thing with the bigger font.
You highlighted the desire for font-size:20px; I inadvertently did font-size:20pt. Just change the pt to px.
Add the div tags to your ipynb file in a Markdown cell:
<div class="bigger">Font size 20px</div>
The result after running the cell:
Step 6)
Somewhere in the notebook, you need to call the link to the CSS file. I just added it to the end:
from IPython.core.display import HTML
def css_styling():
styles = open("./styles/custom.css", "r").read()
return HTML(styles)
css_styling()
It can go at the end because the entire kernel executes initially (so it doesn't need to be before you call the tags.
FYI
I did try to use magic, but it did not work for the font-family:
%%html
<style>
// add your CSS styling here
div.verdana {
font-family:verdana;
}
div.bigger {
font-size: 20pt;
}
</style>
Related
I'm currently learning WebDev with an Instructor from Udemy and I noticed that the behavior of my Atom is not the same as here (Even though I installed the same packages as she did).
The following pictures are from the instructor's screen
When I create an error like the picture below, I get a linter suggestion at the bottom, however, I do not get the pop-up annotation like the picture: Picture1
I also do not receive an alphabetical order suggestion like the picture below in CSS either:
Picture2
I don't receive any errors on HTML pages or see a red dot on the left like this:
Picture3
Instructor's Packages (besides the default ones) : Atom Packages
How can I fix my atom linter?
PS: I did write in the Q&A section of the course but no reply, I'm on Windows 10 if it makes a difference (the instructor is using a mac).
Thanks in advance for the help.
In the first one you should use
h1 { color: red; }
instead of:
h1 { color red; }
With number two I am not quite sure what is meant there and that's why I don't want to say anything wrong.
In number 3, 2 same Id's were used. If you want to use more than one then you should use class instead of id. In CSS you address id's with # and classes with ..
Turns out it was all because Node.JS was not installed.
Once I installed it and ran it, everything started to work.
I use Sphinx with the Read the Docs theme. For small inline code snippets, such as the mention of a single variable among a piece of text, I use double backticks to format the code as what's called "inline literals" in the documentation.
Is this the proper way to format inline code snippets?
Unlike proper code blocks, no language specific syntax highlighting is performed on the inline code snippets (that's fine). As seen in the linked documentation above (see the below screen shot), all such "inline literals" are colored red.
How can I change this color to something else?
I'm only concerned with the HTML output of Sphinx.
I found a solution, though it involves changing the Sphinx RTD theme source code.
The HTML coloring used by the RTD theme is specified in sphinx_rtd_theme/static/css/theme.css, with the sphinx_rtd_theme directory located in the site-packages directory of the Python installation. The red color used is #e74c3c and the specific place which sets this color for inline literals is
literal{color:#e74c3c}
Changing this color does exactly what I want. Making changes directly to theme_css feels wrong though. The specific change takes place on line 4, which is over 100000 characters wide! Clearly this file is generated from other files. I guess the proper solution involves performing the change do one of these (to me unknown) files.
Edit: Proper solution
With a custom .css file, the colors of the inline literals can be set using
code.literal {
color: #404040 !important;
background-color: #fbfbfb !important;
}
The name of a .css file containing the above should be added to html_css_files in the Sphinx conf.py, i.e.
# conf.py
html_css_files = ['custom.css']
This seems like it should be straightforward but I've been prowling the documentation and web and haven't found the answer.
I want to output HTML doc from Sphinx. Ideally I'd like to have three levels of "note" type highlighted text boxes. ReST defines several "admonitions": (http://docutils.sourceforge.net/docs/ref/rst/directives.html#admonitions) but most of the Sphinx HTML themes include special formatting only for Note and Warning. (I am using one of the preinstalled themes, Classic.)
I have two questions:
1) How can I customize the color behind Warning in my documents?
2) How can I add a formatting style for Caution?
I see that these all end up with tags like <div class="admonition warning"> ... in the HTML output. But I can't find where the formatting for that class is defined. Is it in a stylesheet? Is it in a layout.html file or some other file?
Is there anything that explains how the various files in themes actually interact with each other? I haven't found a good primer. (I am no expert on css-based HTML either, so maybe that's part of the problem.)
Okay, I figured out more and have a working workaround. (I'm still not sure how I'm supposed to handle this.)
Looks like my HTML code is reading directly from a few cascading stylesheets stored along with the output in a directory called _static. There's classic.css, which inherits from basic.css.
I don't understand how these relate to the files named like basic.css_t that live in the Python Sphinx install.
To change things, should I (A) try altering the _t files? or (B) create an altered local copy of classic.css that lives in my source directory?
If I go with B, more questions.
Will it be overwritten by the values in the css_t template at build time? (I guess this is easy enough to test)
Is it good practice to use the same filename for a modified version of that stylesheet?
Here's a workaround that avoids those questions and seems to be doing what I want - from this: https://github.com/snide/sphinx_rtd_theme/issues/117
I created an override stylesheet that includes just the formatting I want to change.
I stored it in the _static of my source directory.
I defined it in my conf.py as follows:
html_context = {
'css_files': [
'_static/theme_overrides.css',
],
}
Now, that github discussion said that this wasn't a solution for all kinds of themes (including the RTD theme mentioned in the question) but I think I'm safe for now.
What more should I know?
I am fairly new to python and have no html experience. The question has been asked and either not answered at all or not answered in enough detail for me to set the default font within iPython (not change to browser). Specifically, what has to be put in the css file and which css file should be used? I am on a Windows system.
For reference, these are in answer to the linked SO questions below:
in #1: an unnamed file in /usr/lib/python2.6/.../css/
in comment to #1: change monospace font in browser - worked but font is italic
in #2: custom.css in profile subdirectory /static/custom/custom.css
Related questions:
Change ipython notebook font type
Change font & background color in ipython notebook
Changing (back to default) font in ipython notebook (unanswered) -
Edit:
Changing the monospace font in my browser worked, as suggested in an answer comment of #1. However the font is italic, which is not what is intended.
You can hover to .ipython folder (i.e. you can type $ ipython locate in your terminal/bash OR CMD.exe Prompt from your Anaconda Navigator to see where is your ipython is located)
Then, in .ipython, you will see profile_default directory which is the default one. This directory will have static/custom/custom.css file located.
You can now apply change to this custom.css file. There are a lot of styles in the custom.css file that you can use or search for. For example, you can see this link (which is my own customize custom.css file)
Basically, this custom.css file apply changes to your browser. You can use inspect elements in your ipython notebook to see which elements you want to change. Then, you can changes to the custom.css file. For example, you can add these chunk to change font in .CodeMirror pre to type Monaco
.CodeMirror pre {font-family: Monaco; font-size: 9pt;}
Note that now for Jupyter notebook version >= 4.1, the custom css file is moved to ~/.jupyter/custom/custom.css instead.
In JupyterNotebook cell,
Simply you can use:
%%html
<style type='text/css'>
.CodeMirror{
font-size: 17px;
</style>
I would also suggest that you explore the options offered by the jupyter themer. For more modest interface changes you may be satisfied with running the syntax:
jupyter-themer [-c COLOR, --color COLOR]
[-l LAYOUT, --layout LAYOUT]
[-t TYPOGRAPHY, --typography TYPOGRAPHY]
where the options offered by themer would provide you with a less onerous way of making some changes in to the look of Jupyter Notebook. Naturally, you may still to prefer edit the .css files if the changes you want to apply are elaborate.
The new location of the theme file is: ~/.jupyter/custom/custom.css
In your notebook (simple approach). Add new cell with following code
%%html
<style type='text/css'>
.CodeMirror{
font-size: 12px;
}
div.output_area pre {
font-size: 12px;
}
</style>
Using Jupyterthemes, one can easily change look of notebook.
pip install jupyterthemes
jt -fs 15
By default code font size is set to 11 . Trying above will change font size. It can be reset using.
jt -r
This will reset all jupyter theme changes to default.
In addition to the suggestion by Konrad here, I'd like to suggest jupyter themes, which seems to have more options, such as line-height, font size, cell width etc.
Command line usage:
jt [-h] [-l] [-t THEME] [-f MONOFONT] [-fs MONOSIZE] [-nf NBFONT]
[-nfs NBFONTSIZE] [-tf TCFONT] [-tfs TCFONTSIZE] [-dfs DFFONTSIZE]
[-m MARGINS] [-cursw CURSORWIDTH] [-cursc CURSORCOLOR] [-vim]
[-cellw CELLWIDTH] [-lineh LINEHEIGHT] [-altp] [-P] [-T] [-N]
[-r] [-dfonts]
For chrome users, This is very simple.
Just install the desired font in your OS. Then open the said browser, Go to
Settings -> Appearance -> Customize font.
Go to fixed width font and from drop down list select the desired font.
Note: This might also change the fonts at some other places depending on the web pages that you visit.
There is a much easier way to do without adding the CSS files and all the other methods suggested. But you have to do it every time you start the Jupiter notebook.
Go to inspect in your browser and click on the element selection icon and then click on the box.
And at the bottom of the page, you will be seeing the styling option for CSS where you can easily change the font-size.
I am creating a mobile simulator that mocks the appearance and functionality of an iPhone (and other devices later) in a web browser, using 100% javascript, HTML5, and CSS, with the simulator fully functional with only client side code.
While trying to accomplish this task with as little modification as necessary to the original app projects themselves to be hosted in the simulator, I am injecting the <script> and <link> tags into the head of the page, then loading the html into a <div> screen.
The problem is that when I load in a new css file, it (obviously) overrides the one I'm using to style the page, and therefor some elements are affected (ex the background changes color).
My question is: Is there any way to limit the "scope" of an external .css file to apply only to objects within the <div> screen? Would it make any difference if instead of me injecting it into the <head> of the page, I inject it into a <style> element in the <div> screen?
UPDATE Support for this feature has been dropped. Please seek other options
Original Post:
You may want to look at scoped styles; see http://css-tricks.com/saving-the-day-with-scoped-css/.
The basic idea is
<div>
<style scoped>
#import "scoped.css";
</style>
</div>
However, you are on the bleeding edge here in terms of browser support. See http://caniuse.com/style-scoped.
One alternative would be to use an iframe.
Simply wrap all you css code inside the selector for parent element, say it's a div with id of foo you'd do the following:
div#foo{
//All your css
}
And convert it as less to css, it will prepend the right selectors. Note that you'll need to take care manually of things like #media queries and so on.
While writing this, the <style scoped> is deprecated by the Chrome team.
As a result I experimented with some approaches and released https://github.com/thgreasi/jquery.scopeLinkTags .
Note: you should only have to use this approach in case that you can't control the imported CSS file. If you can use SASS/LESS/anything to pre-process your CSS, you should prefer that.
A simple way is adding pre-class before all selector in css file.
I find a grunt script can do this:
https://github.com/ericf/grunt-css-selectors
This is how i do it if not using preprocessor in my project. Search for a online preprocessor then load copy paste the css under the parent class/id
.parent{
// copy paste here
}
Example
Find a preprocessor for example https://beautifytools.com/scss-compiler.php works very well for me (I have no affiliation with the given link)
if you are using from a URL add the URL using the load URL button.
Wrap the css code under parent and hit compile then minify.
I had a similar issue and found that Shadow DOM can solve it easily.
let output = d.querySelector('#output')
let shadow = output.attachShadow({
mode: 'closed'
});
shadow.innerHTML = HTMLcontent // HTML content and style injected
Source