Autoformat code in a jupyter notebook with a keystroke - jupyter-notebook

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.

Related

Atom or Rstudio like IDE alternative for julia language

Similar to Rstudio, Atom allowed you to run code segments on interactively rather than the entire script all at once. Is there a suitable Julia language IDE that is comparable to rstudio or Atom (juno) and allows for on-the-fly execution of code blocks because Atom is being phased out?
note: Thanks for answers in vs code to obtain interactive feature hold ctrl + return will run code.
Did you try the Microsoft Visual Studio Code ? You can check how to download and setup for Julia notebook in link below.
https://github.com/julia-vscode/julia-vscode#installing-juliavs-codevs-code-julia-extension
The Best, Wr
In Microsoft VS Code you can define code cells using magic comments (## or # %% or #- can play this role - the choice is yours):
##
(your code goes here)
##
A code cell is executed by pressing Alt + Enter while the cursor is inside the cell.
At the code below I pressed Alt + Enter while being in line 10.
The keyboard shortcut can be configured by selecting in the main menu View->Command Palette or pressing Ctrl + Shift + P:
VSCode. The Julia team was working on a Julia IDE called Juno but the website now says "Juno will receive no more feature updates. Development focus has shifted to the Julia extension for VSCode", and VSCode seems to be the recommended way to develop in julia.
The Julia extension for VSCode let's you run code block by just pressing shift+enter and it uses Revise.jl to make interactive sessions better. You can see plots and other outputs right in the editor, and recenty they have even added performance monitoring/benchmarking tools.
However, if you prefer notebook-style workflows, you can use Jupyter with Julia (and the IJulia kernel) but even better imo is Pluto, which is specifically made for Julia and has some nice features such as no hidden state.

DataSpell - save state like in RStudio?

Is it possible to save the Jupyter variables in DataSpell similar to how this works in RStudio?
At the moment, after I closed DS, I need to recompute everything. Of course I can write the main variables to disk myself, but I don't want to do that for all variables all the time.
I think the closest thing to the save/load workspace behavior in RStudio is the %store magic. IPython provides the %store magic, but %store is available in Jupyter Notebooks in DataSpell.
Here is the %store documentation: https://ipython.readthedocs.io/en/stable/config/extensions/storemagic.html

Persistent plotly plots in jupyter notebooks

My jupyter notebooks that have plotly plots do not retain the plots between sessions.
This is running on a Singularity container based on the official jupyter/datascience-notebook docker image with plotly pip installed on top.
I am using the new renderer framework with the notebook renderer.
My notebooks are trusted.
My plots show up during the session without issues.
They persist across refreshes and reloads of the same notebook, even if I restart the kernel.
They disappear either when I restart the jupyter server or sometimes when I reboot the client machine and come back with a new browser session.
The output cells persist with the correct dimensions, but they are blank.
I can see that a whole bunch of js is embedded in the notebook but it does not render in the browser.
At this point, even if I nbconvert to html, they still do not show up.
Tried with Chromium and Firefox.
import plotly.graph_objects as go
import plotly.io as pio
import plotly.express as px
pio.renderers.default='notebook'
then later I plot a bunch of things like:
go.Figure(go.Scattergl(x = var1, y= var2, mode='markers', marker_size=1))
and
go.Figure(go.Histogram2dContour(x = var1, y= var2))
My understanding is that I am set up to retain these figures in offline (non-running) notebooks; the js generated for the plots and the entirety of plotly.js library appears to be embedded in each notebook adding up to 10s of MBs, but they are not rendered.
Due to this issue I end up having to re-run (sometimes expensive) notebooks when all I need is to take a look at a previous plot.
As a recent matplotlib/seaborn convert I absolutely love the interactivity but this is quickly becoming a showstopper at this point.
I feel like I'm missing something.
Any advice is appreciated.
For me, the following solution worked :
in the environment you're using : install orca :
conda install -c plotly plotly-orca
at the beginning of your notebook : override default renderer as 'notebook' :
import plotly.io as pio
pio.renderers.default='notebook'
plotly graphs were persistent between sessions (ie : after restart of kernels) with these modifications.

In Jupyter Notebooks, the tooltip feature (shift + tab) works for some kernels (e.g. Python 3) but not others (e.g. Scala, Groovy). Is that expected?

This is my first foray into Jupyter Notebooks. My background is in Java and Scala development in classical IDEs (e.g. IntelliJ, Eclipse).
I have Jupyter Notebook 5.0.0 installed with only two nbextensions: jupyter-js-widgets and beakerx.
My Python 3 notebooks allow me to use the tooltip feature (i.e. if I were to hit the combination shift+tab inside a method's parenthesis, I get expected arguments). However my Scala and Groovy notebooks do not (but I can run the cells just fine).
Is this expected or am I missing something? If this is expected, how do folks author in the notebooks without the ability to discover method arguments like you can in a standard IDE?
First, you have to run your statements (import module and declarations) by SHIFT + ENTER. Then it will show you tooltip after Shift + Tab was pressed.
See here: https://github.com/twosigma/beakerx/issues/6598
Tl;dr - BeakerX doesn't support this yet. Version 0.10.0 brings us one step closer.

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