Can I use variables on an IPython notebook markup cell? - jupyter-notebook

I have an IPython notebook and I would like to use one of my variables inside a markup cell. Is this even possible? If so, how do you do it?

If you don't mind a code cell that does the job, there is a possibility without adding any extensions.
from IPython.display import Markdown as md
fr=2 #GHz
md("$f_r = %i$ GHz"%(fr))
This will show a markdown cell in a nicely LaTeX formatted output

Currently, this is not possible, however there is a large discussion on this topic here https://github.com/ipython/ipython/pull/2592. The PR is currently closed, but a corresponding issue is opened https://github.com/ipython/ipython/issues/2958 and marked as wishlist.
Update
In the meantime an IPython extension has appeared which allows to render python variables in markdown cells. This extension is part of the IPython notebook extensions and works with IPython 2.x and 3.x. For a detailed description see the wiki page.

It is not officially supported, but installing the python markdown extension will allow you to do so. It is part of the nbextensions, for which you will find installation instructions on their github page. Make sure you'll enable the python markdown extension using a jupyter command or the extension configurator.
Calling python variables then should work with the {{var-name}} syntax, which is described in the readme of the corresponding github page (linked in the wiki):
For example: If you set variable a in Python
a = 1.23
and write the following line in a markdown cell:
a is {{a}}
It will be displayed as:
a is 1.23
Further info on this functionality being integrated into ipython/jupyter is discussed in the issue trackers for ipython and jupyter.

The link: installing notebook extention
gives a clear description of what is necessary to enable the use of variables in markdown cells. Following it, performed the following actions to realize it:
conda install -c conda-forge jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
after a successful completion of the above command I enabled the python markup extension, from jupyter dashboard, as per the following illustration:
Last but not least!!! The NOTEBOOK HAS TO BE TRUSTED to make the markup extension works with python variables
and it worked for me!

Related

Jupyter MathJax Physics package

MathJax3 includes a port of the physics package, which I would like to use inside jupyter notebook / lab.
What are the steps necessary to use this extension? Can it be done without writing custom config files (important for collaboration)?
I tried installing the MathJax3 renderer for jupyter, but it seems that it does not recognize the \require command used to enable the extension. Another user already formulated an issue on github in September, but with no answers yet unfortunately.

JuliaPro: import Jupiter notebook

I usually use Jupyter to have my interactive environment with Julia, now I am switching to JuliaPro, as they claim it is the fastest and easiest way of Julia programming. But, I cannot upload my .ipynb notebooks on JuliaPro. Are they compatible with each other? How can I work with my notebooks on JuliaPro? Thanks!
As was explained in the comments, the .ipynb file format was designed to be rendered in a browser, while Juno/Atom is a text editor that expects a plain text file for display. In general therefore you wouldn't be able to directly use an .ipynb file in Juno.
There is however an option to convert your notebooks to .jl scripts, which is exactly what Juno is expecting: in your Jupyter notebook click on File > Download as > Julia (.jl) (see below)
There's also an answer here that discusses a command line option if you need to batch convert a lot of files.
Also note that your choice of editor / programming environment is unrelated to the version of Julia you're using - while JuliaPro ships with Juno as standard (or potentially the Julia VS Code extension in future), nothing's keeping you from just doing using Pkg; Pkg.add("IJulia"); using IJulia; notebook() in your JuliaPro installation and continuing to work on your notebooks in Jupyter.

How to automatically merge ipython notebooks

I am aiming to create live documentation for a project and I decided to use notebooks.
So far I have created a number of notebooks (one per chapter) and now I want to merge them into one and update it every time I change the individual ones. I also want to create a table of contents.
Is that possible?
I managed to work arround this by creating individual html and pdf versions of my notebooks and then use a python prost-processor(for the html) and pandoc(for the pdf) to merge them. The extra steps allowed me to remove any cells where not necessary in the final html and pdf documents.
If anyone needs it I can post the code too.
Look at NBmerge. You can merge notebooks with nbmerge.
https://github.com/jbn/nbmerge
You must install it with pip. I just tried a conda install nbmerge but it failed because there's no conda package available at Continuum IO or conda-forge. However it is a small utility so installing using pip install should not hurt your conda environment.
pip install nbmerge
and merge your files using the command.
nbmerge file_1.ipynb file_2.ipynb file_3.ipynb > merged.ipynb

workaround for missing --script in ipython notebook 3.0.0-dev

I've been using automaticall saved scripts beside .ipynb files as a convenient way to write modules using ipython notebook. I updated to the head version today, and it seems the --script functionality has been removed.
Is there a way to obtain that functionality (automatically on save) with the latest ipython?
Running ipython notebook gives me a message telling me that it's been removed, and suggests doing
ipython nbconvert --to python [notebook]
To make that work automaticaly, I'd have to trigger it on each save - is there an easy way to achieve that?
Not really a workaround, but the --script functionality was still present in release 2.3.0 (if you're running from a live git repo, you can checkout tag 'rel-2.3.0').

What happened to the TOC extension for ipython notebook?

I'm trying to install the great table of contents extension in a new computer. but I cant find it anymore. the only page I do find does not explain how to install the extension on windows.
So.. How can I install it, and why is it not a part of the official Ipython notebook? I simply can't understand how people are getting along without it.
I've installed toc nbextension successfully with Jupyter 4 (ie. ipython notebook 4) recently.
In fact installing extension is easier than before :)
I post my solution here, may it help.
## download
mkdir toc
cd toc
wget https://raw.githubusercontent.com/minrk/ipython_extensions/master/nbextensions/toc.js
wget https://raw.githubusercontent.com/minrk/ipython_extensions/master/nbextensions/toc.css
## install and enable
cd ..
jupyter-nbextension install --user toc
jupyter-nbextension enable toc/toc
A bit more explain:
install will copy toc to ~/.local/share/jupyter/nbextensions/
enable will modify ~/.jupyter/nbconfig/notebook.json.
You can check these two place to see what happened.
Note: we use enable toc/toc here is because toc.js is in ~/.local/share/jupyter/nbextensions/toc/.
If you put toc.js and toc.css directly in ~/.local/share/jupyter/nbextensions/ then you should use enable toc here.
Edit
Sorry, I didn't notice the orginal problem is on windows. I'm not sure if it's same for windows jupyter, any report is welcome.
Update
Now the toc nbextension has been added into this project which provide a collection of kinds of nbextensions. It's very easy to install and manage, worth to try!
I cannot tell you specific Windows advice, but think the key points should be platform independent:
Create a profile (either a default profile or a named one - you'll probably want default to start).
Locate where the profile is.
Add the custom.js file into the profile.
Edit the custom.js file to point to the notebook extension code.
In a bit more detail, setting up a profile is covered in detail here but for a default profile just go to the command line and enter
ipython profile
Next, locate where your profile is stored by typing at the command line
ipython locate
Call that <profile_dir>.
The rest follows the (Windows equivalent of!) the instructions on the link you have: underneath <profile_dir> navigate to (creating any directories that do not already exist)
<profile_dir>/static/custom/
and add the custom.js file as shown. Then edit the first line, where it has "nbextensions/toc" to point to the location where you have placed the toc.js file you have downloaded. This location is relative to the <profile_dir>; for me I have
<profile_dir>/static/custom/custom.js
<profile_dir>/static/custom/nbextensions/toc.js
<profile_dir>/static/custom/nbextensions/toc.css
and the first line of custom.js reads
require(["/static/custom/nbextensions/toc.js"], function (toc) {
Finally, note that this is with version 1.1.0 of the notebook - if you're using an earlier version I strongly suggest you upgrade before trying this.
You'll also find the official installation instructions at:
https://github.com/minrk/ipython_extensions
These instructions include curl commands for retrieving the toc.js and toc.css files from GitHub, which worked fine for me in a bash shell on linux Mint.
For Windows 7, I used a Git Shell (see http://msysgit.github.io/) to execute the curl commands
This IPython Notebook semi-automatically generates the files for minrk's table of contents in Windows. It does not use the 'curl'-commands or links, but writes the *.js and *.css files directly into your IPython Notebook-profile-directory.
There is a section in the notebook called 'What you need to do' - follow it and have a nice floating table of contents : )
Here is an html version which already shows it:
http://htmlpreview.github.io/?https://github.com/ahambi/140824-TOC/blob/master/A%20floating%20table%20of%20contents.htm

Resources