Shift Tab for showing the documentation of a command in Jupyter Notebook is not working - jupyter-notebook

I have a problem in using the shortcut 'shift tab' in order to get more informations of the package or command I am typing in in a cell. I installed Jupyter notebook via anaconda very recently, I am using python 3.7 and Ubuntu 18.04.
Do you know how to fix this problem ? I googled a lot but could not find a solution.
Many thanks.

Let's say you wrote the below code and trying to get the signature/documentation of function read_csv() with Shift+Tab (It may not work some times)
Code:
import pandas as pd
pd.read_csv()
-> First type only below code
pd.read_csv? ## Execute this code with Shift+Enter
-> Now when you type pd.read_csv and type Shift+Tab, this will show u signature/documentation... This is just a workaround...

Follow two steps:
Step 1: Run that cell first (shift + enter)
Step 2: After running the cell, press the shift + tab.
It worked for me. I hope it will work for you too :)

In the Google Colab environment, if fixed it as follows:
Tools | Settings | Editor |uncheck Automatically trigger code inspection.
Then, Tab and Shift-Tab worked as expected.

I also faced a similar problem but can you confirm that you imported the library in the jupyter notebook and then were calling one of the methods of the library?
What I observed is that if the library wasn't imported into the notebook then the documentation wasn't also showing using Shift+Tab. Once I imported it, then the shortcut was working to show the documentation.
Scenario 1:
import numpy as np #Pressed Enter for next line
a=np.random.randint #Shift + Tab not working
Scenario 2:
import numpy as np #Shift + Enter
a=np.random.randint #Shift + Tab working

Just Run the tab of your code
Then bring your curser in the parenthesis and press shift + tab
press shift + tab + tab for more info

On Google colab:
clicking on the function after running,
move the cursor away and then
bring the mouse and hover it over causes it to pop up.
Note: colab Tools | Settings | Editor | check Automatically trigger code inspection (first setting)

Run again your line of importing the libraries. Now, having loaded your modules you should be able to see the command documentation.

Related

Autoformat code in a jupyter notebook with a keystroke

Is there a widget or keyboard shortcut to apply code formatting a jupyter notebook?
If I have a function like:
def f(x):
y = x*2
z = y*2
return z
I'd like to be able to autotab this function over to:
def f(x):
y = x*2
z = y *2
return z
automatically.
There's jupyter-black - a simple extension for Jupyter Notebook and Jupyter Lab to beautify Python code automatically using Black
For JupyterLab, there's also JupyterLab_Black.
For JupyterLab, there's jupyterlab_code_formatter - a JupyterLab plugin to facilitate invocation of code formatters. You can configure the keyboard shortcut for that.
Keep in mind that soon (present is Fall 2022) the document-centric notebook experience, most people now associate with the classic notebook interface, will be based on what is underlying JupyterLab, see here. And thus investing great time working out things in the old classic notebook may result in you needing to change approaches soon as a lot of the stuff that works only with the classic Jupyter notebook interface will need updating to use while things already working in current JupyterLab will either work or have a lot less friction getting updated.
There is Jupyter Nbextensions Configurator which contains several extensions for your Jupiter Notebook needs:
autopep8;
Code prettify.
You'll find instructions on how to set them up in links.
Both of extensions have their settable toolbar buttons and are applicable to a whole document.

RStudio - How can I have a 3 columns layout?

I'm desperately trying to have as default a 3 columns layout in RStudio and can't manage to obtain it.
I tried to find some documentation, but found nothing with some details or explanation on the "panes" parameters of the rstudio-prefs.json file.
In "C:\Users...\AppData\Roaming\RStudio\rstudio-prefs.json", I saw that it is possible to add a source column using "additional_source_columns": 1 but it is automatically set back to 0 every time I restart R and the problem is that it creates an untitled script in addition to the one I'm opening.
Here are the steps I have to perform to obtain the final layout I'm looking for.
Open a script by double clicking on it
Press CTRL + F7 to add a source column
Drag and drop my script to the left column
remove the 'untitled' script
Thank you for your help ! A link to any documentation that you may know of that I didn't find would be life-saving.
Best
As of today, you can't have a 3 column setup as the default. It's an open Issue on GitHub, marked as an enhancement and added to the "Later" milestone with no due date.
However, with version 2022.02.0+443 (February 2022) of RStudio you can do the following:
Uncheck the option "Restore last opened documents on startup" in Global Options. This will have RStudio open project with no source panes at all.
Using the new option "Open file in Source Column" to open your first file in a new column. This will give you the three columns layout you're looking for.
I would add the "Open file in new column" as a keyboard shortcut, for example CTRL + O. Your workflow would then be:
Open RStudio
CTRL + O to open your first script
Open more scripts any way you want - they will be added to the first column.

How to execute code on Jupyter Notebook when opening it

I don't know if it's possible but I would like to execute Python code inside my Jupyter notebook when opening it.
I know that I can go to Cell >> Run All, but what I am looking for is a way to automatically do it.
1) If you have nbextensions installed, you can designate "initialization cells", which run when the notebook is run. You define them using the cell -> cell type options from the menu.
2) Javascript:
from IPython.display import display, Javascript
display(Javascript("Jupyter.notebook.execute_cells_below()"))
3) For full autonomy from cells you can always import your python code, replacing
cell boundries with function calls.
In Jupyter you have to run the code cell by cell,
another shortcut is SHift+Enter to run.
If you want to run all the codes in same time you can copy it to SPyder and run.

Atom and Hydrogen: Output and console

I'm a Python beginner and recently came across the Atom editor and the package Hydrogen, that implements the Jupyter notebook. I did so after realizing that running the notebook in Chrome consumed way too many resources and also seemed to be a bit slower.
However, the Atom editor and Hydrogen always output prints within a little frame in the code (see image). Unfortunately, it doesn't use the full window width. I also don't see any console/terminal for installing pip libraries.
Is there a way to have the output in a console below the code, just as in Jupyter, and to have a terminal?
this comes most certainly too late, but in case someone else searches for this:
hit control+shift+P / cmd+shift+P and type "toggle output area".

Atom/Sublime like Multiple selections in Jupyter

How can I select matching keywords in a Jupyter notebook via a keyboard shortcut? For example, in the Atom/Sublime editor I can hit cmd + D on a mac (or Ctrl + d on Windows) while the cursor is over 'var' and each time I do that the next 'var' will be highlighted. I can then type the new variable name and 'var' is replaced with whatever I typed.
var = "hello"
print(var)
print(var)
Is there an equivalent in a Jupyter notebook?
Add custom.js to
C:\Users\username\.jupyter\custom # for Windows and
~/.jupyter/custom/ # for Mac
with content
require(["codemirror/keymap/sublime", "notebook/js/cell", "base/js/namespace"],
function(sublime_keymap, cell, IPython) {
cell.Cell.options_default.cm_config.keyMap = 'sublime';
cell.Cell.options_default.cm_config.extraKeys["Ctrl-Enter"] = function(cm) {}
var cells = IPython.notebook.get_cells();
for(var cl=0; cl< cells.length ; cl++){
cells[cl].code_mirror.setOption('keyMap', 'sublime');
cells[cl].code_mirror.setOption("extraKeys", {
"Ctrl-Enter": function(cm) {}
});
}
}
);
and restart jupyter. Now Ctrl+D should work like it does in Sublime.
You can see that Ctrl-Enter functionality is disabled as it would be very convenient to run current cell rather than creating new line for most users. You can choose to have that functionality by commenting that line out.
You can disable other key config that you don't want in a similar way.
Most recent (and easy) way
The best way right now to achieve Sublime-like keymapping in Jupyter Notebook: Select CodeMirror Keymap from jupyter-contrib-nbextensions. As reported in the homepage:
The jupyter_contrib_nbextensions package contains a collection of community-contributed unofficial extensions that add functionality to the Jupyter notebook.
I personally use several extensions from this package and I find them very useful.
As reported in the installation docs, you simply need to run:
pip install jupyter_contrib_nbextensions
to install the extensions (or better, I would suggest:
python -m pip install jupyter_contrib_nbextensions
where python points to the python executable of the installation you are using within Jupyter Notebook). You can also use conda if you prefer.
Anyway, you then need to copy some JS and CSS stuff to make the extensions work within Jupyter Notebook, which you can achieve through:
jupyter contrib nbextension install --user
again, assuming that jupyter points to the jupyter executable you are using to run your notebooks.
At this point, you simply need to enable the extension: navigate the nbextensions_configurator (that comes as a dependency with the jupyter_contrib_nbextensions package), which you can easily do through the Jupyter Notebook dashboard (to be clear, the page you open to run your notebooks) by browsing the Nbextensions tab and check the box corresponding to Select CodeMirror Keymap.
Done! Launching a notebook it will be sufficient to click on Edit>Keymaps>Sublime to achieve the desired behaviour.
I know this is a rather old question, but I happened to come across it before finding out about jupyter_contrib_nbextensions (and in particular the Select CodeMirror Keymap extension). Thus, I decided to post this answer, hopefully to help other people like me and to let them avoid some further search or messing up with customized JS files (which could scary someone).
In jupyter lab now you can add in the extension by searching sublime
Click install and rebuild jupyter.
**Notice: when you click install look at the terminal console, the building result will be shown there
The above solution worked for me, but I found that it had the undesirable effect of entering a "tab" character when I hit enter. Here is the associated GitHub issue: https://github.com/jupyter/notebook/issues/4769#issuecomment-511935127
Per that post, I found that this solution gives the right ctrl + d behavior, and keeps tabs-as-spaces.
require(["codemirror/keymap/sublime", "notebook/js/cell", "base/js/namespace"],
function(sublime_keymap, cell, IPython) {
// setTimeout(function(){ // uncomment line to fake race-condition
cell.Cell.options_default.cm_config.keyMap = 'sublime';
var cells = IPython.notebook.get_cells();
for(var c=0; c< cells.length ; c++){
cells[c].code_mirror.setOption('keyMap', 'sublime');
}
// }, 1000)// uncomment line to fake race condition
}
);
In Jupyter Lab, this can now be set in Settings > Text Editor Key Map > Sublime Text.
The only solution which made this work for me is
pip install jupyterlab_sublime

Resources