How to format text cells in Jupyter? - python

I would like to create a Jupyter notebook to make a tutorial.
When I go to the jupyter website and I try jupyter in the browser, there is already a nice python tutorial, with some cells used as text explanations and others as code:
Now I create a new notebook and I am able to create different cells and format them in 4 types:
Code
Markdown
Raw NBConvert
Heading
However none of them allow me to create the text explanations with white background as in the image above.
I tried formatting one cell as markdown and it's just keeping with the grey background.
Am I missing something? Thanks.

After creating those blocks you have to run them to give you that white background.
Press Ctrl + Enter inside the block.
For eg.
Click on a cell. Press Esc and then press 1
Now type this: "THIS IS A HEADING"
And then press Ctrl + Enter.
You will get this as an output with a white background (The image below is a bit dull):

In addition, by using the %%html magic command, you can type HTML in cells and get them rendered the way you want

Related

VS Code Jupyter Notebooks - How to stop result text wrapping?

When using Jupyter Notebooks within VS Code is it possible to stop the code cell result text from wrapping?
When querying wide dataframes (Eg 100 columns wide) the resulting text is unreadable due to the text wrapping and requires copying out to other text editors to be able to read the results.
Attached is a screenshot of a simple dataframe header that is wrapping over a couple of lines, once you're returning actual rows it's unusable.
Screenshot
This could be a limitation of Jupyter's UI. I recommend you to see your data in variable explorer and data viewer. By clicking the Variables icon in the top side after running, you'll see a list of the current variables. For additional information of the variables, you can double-click on a row or use the Show variable in data viewer button to see a more detailed view.

Inserting comments into jupyter notebook

I hope this is not a redundant question, but I was not able to find a proper answer and tutorial. I am currently learning Data Science and therefore use kaggle competition for practice and jupyter notebook to tackle them (as it is visually more appealing to me). Now I stumbled upon an example kernel (see: http://nbviewer.jupyter.org/github/agconti/kaggle-titanic/blob/master/Titanic.ipynb#Data-Handling) is using comments, links and illustrations between his codes to give more context.
Is anybody aware of how to write such comments and remarks within jupyter? If so, do you have an online resource where I could learn this?
Thanks a bunch!
Yep - highlight a cell and click on the "Cell" dropdown menu and go to "Cell Type" and choose "Markdown". Then you can type any markdown text you'd like and it will render as such.
^^ Also there are shortcuts for changing cell types as well. Highlight a cell and press the esc key to change into "command" mode and press m. This changes it to a markdown cell. Press y to change it back to a code cell.
You can also do latex equations using dollar signs ($). There is documentation for this here
For markdown cells
[//]: # "Your comment in here."
allows to include a comment in jupyter notebook server version 6.0.1. See this discussion for more details on markdown comments.
Agree with #RSHAP. I just add the flow picture in the below.
select the active Cell > Cell Type > Markdown.
irrespective of the version of jupyter notebook you use, the following command should work on current cell
Pres ESC
then Press m
As mentioned in the above remarks, use markdown cells.
Once in a markdown cell, use #,##,###,#### for headings of different fonts. For bold, use **your_text_here**, and for italics use *your_text_here*. For writing equations and using latex commands, use $ symbol. Everything works just like in latex; you can even define your notations like you would normally do in Latex. For a more detailed and structured overview to format text In Jupyter, have a look at this website:
https://www.earthdatascience.org/courses/intro-to-earth-data-science/file-formats/use-text-files/format-text-with-markdown-jupyter-notebook/

ipython notebook clear all code

All I want to do is try some new codes in ipython notebook and I don't want to save it every time as its done automatically. Instead what I want is clear all the codes of ipython notebook along with reset of variables.
I want to do some coding and clear everything and start coding another set of codes without going to new python portion and without saving the current code.
Any shortcuts will be appreciated.
Note: I want to clear every cells code one time. All I want is an interface which appears when i create new python file, but I don't want to save my current code.
In Jupyter, do the following to clear all cells:
Press Esc to enter command mode.
Hold Shift. Select the first and last cells to select all cells.*
Press d twice to delete all selected cells.
Alternatively, if you simply want to try out code, consider running the ipython console, which is purely interactive in the REPL and does not require creating a new file. In a command prompt, type:
> ipython
Demo
Click outside the textbox to select a cell in command mode (thanks #moondra's).
Command Mode (Yes)
Edit Mode (No)
Well, you could use Shift-M to merge the cells (from top) - at least there's only one left to delete manually.
You can type dd to remove current selected cell. And if you continously press dd you can clean the screen.
Press the scissors (cut) button multiple times for each cell you want to delete.
Just do Ctrl+A , this selects all the individual blocks/cells and then press d twice.
Ok youngster, I will break this down for ya in simple steps:
go to the intended textbox and put your mouse cursor there
on keyboard press crtl-a and delete
Ta-dah all done
Glad to help

Ipython Notebook: Elegant way of turning off part of cells?

In my ipython notebook, there is part of cells that serves as preliminary inspection.
Now I want to turn it off, since after running it I know the status of the dataset, but I also want to keep it, so other people using this notebook can have this functionality.
How can I do it? Is there any example of doing it?
I can comment out these cells, but then switching between on and off would be quite laborious. And may not be quite convinent for other people.
I can abstract it into a function, but that itself has some methods, so the code would be quite convoluted, and may be hard to read?
Using Jupyter notebook you can click on a cell, press esc and then r. That converts it to a "raw" cell. Similar thing can be done to convert it back, esc + y. No comments needed, just key presses.
Within Jupyer notebook, go to Help -> Keyboard shortcuts for more.
Here's a snippet:
Command Mode (press Esc to enable)
↩ : enter edit mode
⇧↩ : run cell, select below
⌃↩ : run cell
⌥↩ : run cell, insert below
y : to code
m : to markdown
r : to raw
In Jupyter notebooks one can use this magic preamble at the beginning of a cell to avoid its execution:
%%script false --no-raise-error
You can use a condition at the cost of one extra indentation.
cellEnabled = 0
#cellEnabled = 1
if cellEnabled:
doA()
doB()
I had the same kind of desire and I eventually found out about the nbextension called Freeze. When you enable it, you get a nice freeze button in your toolbar. When you click it, the cell you're currently in will become "frozen". This means it will turn green (making it visually clear) and it will be ignored by the Run All process. It's also locked for editing, so you do need to unfreeze it (unlock button, two over to the left of the freeze button) before editing or running the cell. That's really easy to do though because it's just one button.
Let me know if this wasn't super clear. Otherwise, I hope this helps!

Writing a header in iPython Notebook causes the header to be invisible

In iPython Notebook running Python 2.x, when we write a markdown #header in a new cell and execute it, nothing appears
Every word I've tried seem to work for the header except header. How can we have a header with the word header?
I get the same issue. Here is a workaround.
<h1> header
I get the same problem in jupyter. Saving the notebook does not make the '#' markup symbol go away and leaves the text in a garish blue color, as you illustrate with the image you shared.
The only way I can get the header to assume its proper appearance is to close the notebook and reopen it. Then the text appears as it should.

Categories