Markdown Cell Jupyter Shortcut



Markdown

In one of my post, I had written about installing Jupyter Lab and configuring it. If you haven’t installed it yet, you can check this post for the information. I have been using these Jupyter lab shortcuts and magic functions in my day to day work and have explored some of the shortcuts and tips of working with it. These have made my experience better with Jupyter lab and have made me more productive. If you find it helpful then bookmark this page for a quick reference in the future.

Shortcut Keys for Jupyter lab

Jupyter Lesson 5: Working with Markdown Cells MarkDown Cells are used to present text, images, files and LaTex syntax. Content in Markdown cells are written in markdown language which is a superset of HTML. Markdown Cells Text can be added to Jupyter Notebooks using Markdown cells. You can change the cell type to Markdown by using the Cell menu, the toolbar, or the key shortcut m. Markdown is a popular markup language that is a superset of HTML.

While working with any tools, it helps if you know shortcut key to perform most frequent tasks. It increases your productivity and can be very comfortable while working. I have listed down some of the shortcuts which I use frequently while working on Jupyter Lab. Hopefully, it will be useful for others too. Also, you can check full list of shortcut by accessing the commands tab in the Jupyter lab. You will find it below the Files on the left hand side.

  1. ESC takes users into command mode view while ENTER takes users into cell mode view.
  2. A inserts a cell above the currently selected cell. Before using this, make sure that you are in command mode (by pressing ESC).
  3. B inserts a cell below the currently selected cell. Before using this make sure that you are in command mode (by pressing ESC).
  4. D + D = Pressing D two times in a quick succession in command mode deletes the currently selected cell.
  5. Jupyter Lab gives you an option to change your cell into Code cell, Markdown cell or Raw Cell. You can use M to change current cell to a markdown cell, Y to change it to a code cell and R to change it to a raw cell.
  6. CTRL + B= Jupyter lab has two columns design. One column is for launcher or code blocks and another column is for file view etc. To increase workspace while writing code, we can close it. CTRL + B is the shortcut for toggling the file view column in the Jupyter lab.
  7. SHIFT + M = It merges multiple selected cells into one cell.
  8. CTRL + SHIFT + – = It splits the current cell into two cells from where your cursor is.
  9. SHIFT+J or SHIFT + DOWN = It selects the next cell in a downward direction. It will help in making multiple selections of cells.
  10. SHIFT + K or SHIFT + UP = It selects the next cell in an upwards direction. It will help in making multiple selections of cells.
  11. CTRL + / = It helps you in either commenting or uncommenting any line in the Jupyter lab. For this to work, you don’t even need to select the whole line. It will comment or uncomment line where your cursor is. If you want to do it for more that one line then you will need to first select all the line and then use this shortcut.

Jupyter Lab Magic Functions

Magic functions are very specific to Jupyter lab or Jupyter notebook and are provided by the IPython kernel. It adds extra functionality to our Jupyter lab notebooks which is not limited by our choice of language. This also provides some of the IDE type functionality to Jupyter Lab.

  1. %matplotlib inline = Most people must be already knowing about this. It allows the output of plotting command to be displayed inline i.e. in Jupyter lab UI.
  2. %lsmagic =It lists all the available magic function for the Jupyter lab. It can be useful if you want to explore all the available magic functions.
  3. %env =It lists all the environment variable. It can be used in the following way:-
    • %env: lists all environment variables and its values
    • %env var: gives value for var environment variable
    • (%env var val) or (%env var=val): sets value for var environment variable
  4. %run = can run .py and .ipnyb code file in the cell.
    • %run file_name
  5. %load = loads code into the cell. It can use various source for this i.e. filename, URL etc.
    • %load source
  6. %who = will list all variables that exist in the global scope. It can be used to see what all data_frames or any other variable is there in memory.
    • %who: all variables present
    • %who df: lists all the data frames present
    • %whos: same as %who, but it gives more information about each variable
  7. %time = will give you information about the time taken in a single run of the code in your cell.
  8. %timeit = uses the Python timeit module which runs a statement 100,000 times and gives the average time taken.
  9. %writefile = writes the contents of the cell to a file
    • %writefilefile_name
    • %writefile -afile_name: This will append contents of the cell to an existing file
  10. We can use IPython Magics with the name of a kernel at the start of each cell that we want to use that Kernel for. For e.g. %%bash %%HTML %%python2 %%python3 %%ruby %%perl

Bold In Jupyter Markdown

Change in Jupyter configuration file

CellMarkdown

In Jupyter Lab, cells print O/P of the latest print statement. If we use more than one print statement, it overrides the previous O/P. But we can change it so that cells show all the output by running the below code

This won’t change the behavior permanently and we will need to run above code in every notebook. We can make it as a default behavior by changing the configuration file. We will need to add this in the configuration file. You can check this post to know about Jupyter Lab configuration file and how to make changes in it.

These are few of the options available to a user in terms of both magic functions and shortcut. You can explore more by visiting the official page about IPython magic functions and find more which is needed for your working. If you use any other magic functions or shortcut which can be useful to others, let us know in the comment section.

Jupyter notebook shortcut markdown cell
main.js
// Allow sphinx rst references in markdown cells
// TODO: Markdown cells will only be reevaluated when a notebook is dirty
// (i.e. you have made changes). If you save it before reevaluating MD cells,
// they will show the old value.
define([
'base/js/namespace',
'jquery',
'notebook/js/cell',
'base/js/security',
'components/marked/lib/marked',
'base/js/events',
'notebook/js/textcell'
],function(IPython,$,cell,security,marked,events,textcell){
'use strict';
/*
* Find Sphinx expressions and add to text as <a href> tags
*
* @method execute_sphinx
* @param cell {Cell} notebook cell
* @param text {String} text in cell
*/
varexecute_sphinx=function(cell,text){
/* always clear stored variables if notebook is dirty */
if(IPython.notebook.dirtytrue)deletecell.metadata.sphinx_links;
// search for code in mangled <code> blocks
varfound=false;
varnewtext=text.replace(/:ref:<code>(.*?)s*&lt;(.*?)&gt;</code>/g,function(match,info,link,cha){
found=true;
link=link.replace(/.html$/,');
if(link.indexOf('.')-1){
link=link+'.ipynb';
}
console.log(link);
return'<a href='+link+' target=_new>'+info+'</a>';
});
if(found){
if(typeofcell.metadata.sphinx_links'undefined'){
cell.metadata.sphinx_links={any: true}
}
}
returnnewtext
};
/*
* Render markdown cell and replace {{...}} with python code
*
*/
varrender_cell=function(cell){
varelement=cell.element.find('div.text_cell_render');
vartext=execute_sphinx(cell,element[0].innerHTML);
if(text!undefined){
element[0].innerHTML=text;
MathJax.Hub.Queue(['Typeset',MathJax.Hub,element[0]]);
}
};
/* force rendering of markdown cell if notebook is dirty */
varoriginal_render=textcell.MarkdownCell.prototype.render;
textcell.MarkdownCell.prototype.render=function(){
if(IPython.notebook.dirtytrue){
this.rendered=false
}
returnoriginal_render.apply(this)
};
varload_ipython_extension=function(){
events.on('rendered.MarkdownCell',function(event,data){
render_cell(data.cell)
});
/* show values stored in metadata on reload */
events.on('kernel_ready.Kernel',function(){
varncells=IPython.notebook.ncells();
varcells=IPython.notebook.get_cells();
for(vari=0;i<ncells;i++){
varcell=cells[i];
if(cell.metadata.hasOwnProperty('sphinx_links')){
render_cell(cell);
}
}
});
};
return{
load_ipython_extension : load_ipython_extension
};
});
Markdown

Markdown Cell Jupiter Shortcut Commands

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment