Remove cells from Jupyter Notebook with nbconvert - jupyter

Recommendations mentioned in How to hide one specific cell (input or output) in IPython Notebook? don't work.
On Windows I do the following
jupyter nbconvert a.ipynb --TagRemovePreprocessor.remove_cell_tags="{'remove_cell'}"
but get an error
traitlets.traitlets.TraitError: The 'remove_cell_tags' trait of a TagRemovePreprocessor instance must be a set, but a value of type 'unicode' (i.e. u"{'remove_cell'}") was specified.
I also tried '{"remove_cell"}'
I am using nbconvert 5.4.0
Any ideas how to do this?

You need to enable the TagRemovePreprocessor before you call it.
The code below shows how to enable it and how to enclose your tags as a list so you can exclude more than one tag if you wish. To exclude a single tag, just put one element in the list eg ['remove_cell'].
The parameter --to html is not required if you are converting to html (as html is the default). If you want to convert to python, for example, change --to html to --to python
jupyter nbconvert a.ipynb --TagRemovePreprocessor.enabled=True --TagRemovePreprocessor.remove_cell_tags="['remove_cell', 'other_tag_to_remove']" --to html
Note that the TagRemovePreprocessor is only available in nbconvert 5.3 and above: https://nbconvert.readthedocs.io/en/latest/changelog.html?highlight=TagRemovePreprocessor

Needs some extra quoting to work:
--TagRemovePreprocessor.remove_cell_tags={\"remove_cell\"}.
However beware of an ongoing issue with noteboot to notebook conversion - it seems like in this case preprocessors, including tag removal, do not run. See more in this SO question:
jupyter nbconvert --to notebook not excluding raw cells
Update: Not tested on windows, just on Linux

Related

Pandoc Jupyter (.ipynb) to Word (.docx) incorrect results

Jupyter notebook (ipynb) to Word-format document (.docx) conversion just is not working correctly. I have tried several approaches using jupyter nbconvert, pandoc, and commercial document format converters. So far, none have produced appropriate results. I have to believe there exists some way for pandoc to do the conversion correctly. Thank you so much for your assistance on this.
The Word-format document should preserve these aspects of the Jupyter notebook:
Headings numbered
Code cells hidden
Latex math expressions presented correctly
Tables with images presented correctly
Data.frame presented in same format as in Jupyter (pretty row-banded table)
Kable data.frame presented in same format as in Jupyter (pretty row-banded table)
Here attached are a test Jupyter notebook and examples of some (inappropriate) results.
PrintTest.ipynb is the Jupyter notebook to be transformed to Word-format.
PrintTest.jpg is a screenshot of the Jupyter notebook.
PrintTest_1.html from Jupyter, File > Download as > HTML Embedded (.html)
No heading numbers
Code cells exposed
PrintTest_2.html jupyter nbconvert PrintTest.ipynb --to=html --template=toc2 --output PrintTest_2.html
Sidebar table of contents exposed
PrintTest_3.docx pandoc PrintTest.ipynb -o PrintTest_3.docx
Latex math expression presented as plain text
Table with image not presented
Code cells exposed
Data.frame presented as plain text
Kable data.frame not presented
PrintTest_4.docx pandoc PrintTest_2.html -o PrintTest_4.docx
Latex math expression presented as plain text
Table with image not centered
Data.frame presented as plain text
Kable data.frame presented as plain text
PrintTest_5.docx pandoc PrintTest.ipynb --mathjax -o PrintTest_5.docx
Same as PrintTest_3.docx
PrintTest_6.docx pandoc PrintTest_2.html --mathjax -o PrintTest_6.docx
Same as PrintTest_4.docx
This post by a team targeting the issue of writing in Jupyter and exporting to Word might interest you.
With Pandoc there is a flag -N for some output writers (word, html, ...) to number the headings.
Example:
pandoc jupyter_file.ipynb -s -N -o new_word_file.docx
add --toc if you also want to export a table of content:
pandoc jupyter_file.ipynb -s -N --toc -o new_word_file.docx

Inserting a (python) cell when calling Jupyter nbconvert

Is it possible to make nbconvert insert a cell prior to processing the notebook?
Or perhaps detect when it's being run through nbconvert or web interface
Expanding a bit on the why:
MathJax lacks siunitx, so in order for me to see understandable math in the notebook and output better-looking units (or customize through a template) in pdf I'd like to resort to adding a command-line argument to nbconvert, using the printviewlatex extension, so that it inserts a python cell that changes the notebook behaviour to output latex instead of markdown.
I realize that there's an siunitx MathJax extension (which I failed miserably to install in jupyter), but this solution may also solve other problems.

Ipython Notebook writefile and execute cell at the same time

When using the magic cell %%writefile, it seems that ipython treat the content of the cell as plain txt and will not execute it.
Is there any way to run the code in the cell and then automatically export it to some script file?
This is useful because it gives you the ability to programmatically sync your notebook with a script that only keep some parts of the notebook.
In the notebook, some codes are only scaffolds, downloading the notebook as a py script will include these unnecessary codes. Meanwhile, you may want to re-structure the document, put two code blocks together while in the notebook there are a lot of draft content between them.
Ugly, all of the above things can be done by copying codes manually. But it will make future maintenance full of tedious recopy and paste work.
Thanks to author for answer. I think full code will be useful for others:
from IPython.core.magic import register_cell_magic
#register_cell_magic
def write_and_run(line, cell):
argz = line.split()
file = argz[-1]
mode = 'w'
if len(argz) == 2 and argz[0] == '-a':
mode = 'a'
with open(file, mode) as f:
f.write(cell)
get_ipython().run_cell(cell)
You can declare this in your Jupiter notebook. Mark cells like:
%%write_and_run some.py
or
%%write_and_run -a some.py
Well, seems that those using ipython don't care my question. I think I have sorted out the solution by myself.
Actually, you can write your own magic command following this post. The key command to use related to ipython is
self.shell.run_cell(cell)
You can write a magic command that firstly executes the cell using the above command and then save the cell content to some file. Ipython will pass the line and cell args as unicode object.
I have implemented one that does the following job.
run the code
write the code to some file given by -f arg
if a -before arg(a str) is given, then put the code just before that str.
if an -indent arg is given, then write a indented version of cell

IPython (Jupyter) MathJaX preamble

Question
How can I setup a MathJax "preamble" for use in IPython (or Jupyter) notebooks for repeated use in a way that is convenient for others to read my documents (on http://nbviewer.org) and that works for LaTeX/PDF generation?
Background
I would like to use IPython (now Jupyter) notebooks for documents that I later convert to PDF via LaTeX (using ipython nbconvert). The problem is how to include a bunch of macro definitions that I use in almost every document. Something like:
\newcommand{\vect}[1]{\vec{#1}}
\newcommand{\abs}[1]{\lvert#1\rvert}
\DeclareMathOperator{\erf}{erf}
etc. As far as the notebooks is concerned, one unsatisfactory solution is to simply include these in a markdown cell at the top of the notebook, embeded between two dollar signs $$ so it is interpreted as math. If this is done after some introductory text, then it does not even affect the output.
The problem is that, when converting to LaTeX (for PDF export), these commands are embedded in a math environment in the LaTeX file. This has several problems:
Commands like \DeclareMathOperator must come in the LaTeX document preamble.
Command definitions are local to the equation and not available later in the document. (This can be overcome by using \gdef or \global\def but then one must trick MathJax into recognising these commands with something like \let\gdef{\def} which is somehow hidden from LaTeX. Any way I have found of making this work amounts to an ugly hack.)
Sometimes commands are already defined in LaTeX and need to have \renewcommand (not supported by MathJax, but again can be provided by \let\renewcommand\newcommand etc. which seems reasonable to me since MathJax can't have some idea of what preamble might be used for the final LaTeX file).
Probably the solution is to provide a set of macros to MathJax by adding code like (not sure the equivalent of \DeclareMathOperator here...)
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
Macros: {
vect: ["{\\vec #1}",1],
abs: ["{\\lvert #1 \\rvert}",1]
}
}
});
</script>
to a custom.js file and then providing a LaTeX package for inclusion when converting to PDF. The problem I have with this approach is: How to distribute the custom.js file and LaTeX style file for others (collaborators and viewers) to use?
I want collaborators to be able to edit and read my documents without having to install custom extensions in their global configuration. To be specifiec, I am fine with requiring them to run a command like python setup.py configure once they download/checkout my code which does local modifications to the project like populating ipython_notebook_config.py files in all directories containing notebooks, but am not happy installing extensions, or modifying their personal global custom.js file.
My stumbling block here is that I don't know how to add contributions from a local custom.js file to the notebook chain, and suspect that this might violate a security policy.
The best solution would not require any action on my collaborator's part.
I want my notebooks to work on http://nbviewer.org, and for people to be able to download the notebook and produce a PDF. (I think this rules out the possibility of using custom.js hacks and a distributed *.sty file, but am not certain.)
I would prefer to be able to simply start a new notebook and then start writing without having to insert a bunch of boilerplate code at the start of each notebook, though would be amenable to having a simple way of automating this process using an notebook extension or some hooks in python_notebook_config.py.
References
The following posts address some of these issues, but fall short on most fronts:
usepackage and making macros in ipython notebook
Physics bra-ket symbols in IPython (specifically this answer notes related difficulties)
How do I get MathJax to enable the mhchem extension in ipython notebook
Discussions about (potential) problems with the pandoc production of LaTeX files from IPython notebooks:
Getting some problems with pandoc and mathjax
\newcommand environment when convert from markdown to pandoc
Pandoc IPython notebook loses some Mathjax
General discussion of math in notebooks:
How to write LaTeX in IPython Notebook?
I think you can solve some of your problems, but not all.
First, the stumbling block. I believe (though I might be wrong) that nbviewer doesn't look at anything but the notebook itself. For example, I don't see how it could run an ipython_notebook_config.py stored alongside your notebook. So that rules out that line of thought, meaning that I think you'll have to bite the bullet and add boilerplate to every notebook. But you might at least be able to minimize the boilerplate. In that vein:
You could maintain your custom.js (probably under a more descriptive name) on github or whatever, and then add one line of boilerplate to all your notebooks to load that script from the URL. You would still need boilerplate, but it would be a lot shorter.
Once you have executed the code cell containing the javascript, it is saved in the notebook, which means that it will automatically happen the next time the browser loads it, even before the code cell is executed. So unless nbviewer prevents the javascript's execution, it should work just fine. This would also make things work nicely for collaborators, since they wouldn't have to download additional files.
As for your own style file, I suspect that anyone sophisticated enough to install ipython and latex, download your notebook, and run nbconvert on it would also be sophisticated enough to download the .sty file. Anyway, I don't see any way around the need to do that...

display math on nbviewer?

I write an ipython notebook which correctly display the equations on my local machine. However, when I paste the ipynb file to Gist and use the nbviewer to view it, some math equation disappear. What causes the problem? Any way to fix it? The ipynb has the the following latex code in Markdown cell:
\begin{align}
F(P)=f_L(P)+f_G(P_{i,j})+f_{elec}(P,\phi_{ext},\phi_{int})\qquad (1)
\end{align}
The problem you are facing is that nbviewer uses nbconvert to convert the ipynb to html. Nbconvert inturn uses pandoc to do the conversion and pandoc strips raw latex (the \align in your case) when converting markdown to html.
You can try to embed the raw LaTeX into $s to make pandoc aware, however not all constructs are supported and will be converted (see Github Issue for more details).