Convert jupyter notebook to pdf: Error encounters when install pandoc - python

I was trying to convert .ipynb to a pdf.
I'm on Windows and use Jupyter via IE browser.
I'm following the link http://pandoc.org/installing.html to install Pandoc.
Here's what I have done: Any suggestions?
Install MikTex: http://miktex.org/download
Install pandoc
Install stack:
-got to cmd from pandoc folder, run stack setup
-stack init
-stack install---->ERRORS!!

If you would like an alternative to it instead of using nbconvert what you can do is :
Download your ipynb file as HTML from File option.
Right-click and select print or use Ctrl+P.
Save as PDF
Easy.

Related

Mac - VSCode - Converting Python File to PDF Error

After pip3 installing nbconvert and jupyter and installing MacTex, I still have the following error, which wouldnt allow me to export to PDF:
Could anyone provide assistance? Thank you
You can export it to HTML and then using the Print to PDF feature of your browser like the message suggested.
If you insist to export it to PDF directly. After you install the MacTex you need to restart your computer, and make sure the installation path of MacTex can be found in your VSCode terminal, xelatex command can be recognized. Like this picture displays(Windows):

Running python script in cmd does not give any output [duplicate]

I have some code in a .ipynb file and got it to the point where I don't really need the "interactive" feature of IPython Notebook. I would like to just run it straight from a Mac Terminal Command Line.
Basically, if this were just a .py file, I believe I could just do python filename.py from the command line. Is there something similar for a .ipynb file?
nbconvert allows you to run notebooks with the --execute flag:
jupyter nbconvert --execute <notebook>
If you want to run a notebook and produce a new notebook, you can add --to notebook:
jupyter nbconvert --execute --to notebook <notebook>
Or if you want to replace the existing notebook with the new output:
jupyter nbconvert --execute --to notebook --inplace <notebook>
Since that's a really long command, you can use an alias:
alias nbx="jupyter nbconvert --execute --to notebook"
nbx [--inplace] <notebook>
From the command line you can convert a notebook to python with this command:
jupyter nbconvert --to python nb.ipynb
https://github.com/jupyter/nbconvert
You may have to install the python mistune package:
sudo pip install -U mistune
In your Terminal run ipython:
ipython
then locate your script and put there:
%run your_script.ipynb
You can export all your code from .ipynb and save it as a .py script. Then you can run the script in your terminal.
Hope it helps.
Using ipython:
ipython --TerminalIPythonApp.file_to_run=<notebook>.ipynb
Normally, I would prefer this option as it is really self-describing. If you prefer to use less characters, use:
ipython -c "%run <notebook>.ipynb"
which is basically what Keto already suggested (unfortunately a little bit hidden) as a comment.
In my case, the command that best suited me was:
jupyter nbconvert --execute --clear-output <notebook>.ipynb
Why? This command does not create extra files (just like a .py file) and the output of the cells is overwritten everytime the notebook is executed.
If you run:
jupyter nbconvert --help
--clear-output
Clear output of current file and save in place, overwriting the existing notebook.
For new version instead of:
ipython nbconvert --to python <YourNotebook>.ipynb
You can use jupyter instend of ipython:
jupyter nbconvert --to python <YourNotebook>.ipynb
Update with quoted comment by author for better visibility:
Author's note "This project started before Jupyter's execute API, which is now the recommended way to run notebooks from the command-line. Consider runipy deprecated and unmaintained." – Sebastian Palma
Install runipy library that allows running your code on terminal
pip install runipy
After just compiler your code:
runipy <YourNotebookName>.ipynb
You can try cronjob as well. All information is here
I had the same problem and I found papermill. The advantages against the others solutions is that you can see the results while the notebook is running. I find this feature interesting when the notebook takes very long. It is very easy to use:
pip install papermill
papermill notebook.ipynb output.ipynb
It has also, other handy options as saving the output file to Amazon S3, Google Cloud, etc. See the page for more information.
You can also use the boar package to run your notebook within a python code.
from boar.running import run_notebook
outputs = run_notebook("nb.ipynb")
If you update your notebook, you won't have to convert it again to a python file.
More information at:
https://github.com/alexandreCameron/boar/blob/master/USAGE.md
There is now a jupyter run subcommand that will execute a notebook.
jupyter run notebook.ipynb
More on this command can be found in the Jupyter documentation.
From the terminal run
jupyter nbconvert --execute --to notebook --inplace --allow-errors --ExecutePreprocessor.timeout=-1 my_nb.ipynb
The default timeout is 30 seconds. -1 removes the restriction.
If you wish to save the output notebook to a new notebook you can use the flag --output my_new_nb.ipynb
You can also use jupytext https://jupytext.readthedocs.io/en/latest/index.html.
This allows you to pair your notebook. So for each ipynb file you have a .py file as well with some comments. The .py file can be executed as usual.
You can enjoy benefits of two worlds with the cost of one extra file though.
Oh, and by the way if you are using version control you can only commit .py files with a nice diff instead of the ugly .ipynb diffs.
(The syntax in the .py files is similar to Databricks notebooks iy you are familiar with them...)
In a batch file paste the below. Use ipython to run .ipynb files.
#echo on
call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
ipython "path\test.ipynb"
pause
I've made some research on this topic and wrote an article on 4 ways to run Jupyter Notebook in command line below is summary of my findings.
1. Use nbconvert
The nbconvert package is already installed in Jupyter Notebook. It can be used to execute notebook. Additionally it has many features:
can export notebook to PDF or HTML,
can hide code in output notebook,
can execute notebook even with errors in cells.
Example notebook execution:
jupyter nbconvert --execute --to notebook --allow-errors your-notebook.ipynb
The above command will output your-notebook.nbconvert.ipynb file and will execute all cells even with errors.
2. Use papermill
The papermill allows you to parametrize notebook. You can define variables as parameters (with cell tags).
Example command:
papermill -p name Piotrek your-notebook.ipynb output-notebook.ipynb
Example notebook with injected parameters:
3. Manually download notebook as .py script
There is an option to manually download notebook as .py script:
After download you can add execution rights to the file and run it as a command line script.
4. Use jupytext
The jupytext package allows you to synchronize .ipynb file with .py file. You don't need to manually convert notebook to script.

How could I run ipynb file on my device like a game icon?

I know this question might sound very primitive
How could U run an ipynb file on a device that doesn't have jupyter or python installed?
to ask the question properly (which format do I need to convert my ipynb file to so it could run normally as if it was a game without the need to install python or Jupyter?) (like .exe when you install a program for example)
If not available then can I run it on web ? wihtout showing the coding just an interactive mode?
What you can do is to first, covert your ipynb into a python file, then convert that python file into and .exe:
1. Convert ipynb into .py
pip install nbconvert
jupyter nbconvert --to script your_notebook.ipynb
The output of the above is your_notebook.py
2. Convert .py to .exe
pip install pyinstaller
pyinstaller your_notebook.py
You should now have a your_notebook.exe which you can open by double click

Convert ipynb notebook to HTML in Google Colab

I have a Google Colaboratory Notebook for Data Analysis that I want to output as a HTML file as currently not everything loads within the Colab environment such as large Folium Heatmaps. Is it possible to export the notebook as a html file as opposed to the ipynb and py options?
Method using Google Colab only
Download your .ipynb file
You can actually do it using only Google Colab. File -> Download .ipynb
Reupload it so Colab can see it
Click on the Files icon on the far left:
Then Upload to session storage:
Select & upload your .ipynb file you just downloaded.
Get your file's path
then obtain its path (you might need to hit the Refresh button before your file shows up):
Conversion using %%shell
Then, just as in Julio's answer, execute in a Colab cell:
%%shell
jupyter nbconvert --to html /PATH/TO/YOUR/NOTEBOOKFILE.ipynb
The %%shell lets the interpreter know that the following script is interpreted as shell. Don't write anything before %%shell, use a distinct cell for this.
The form of /PATH/TO/YOUR/NOTEBOOKFILE.ipynb will be something like /content/lightaberration3.ipynb.
Your file is ready
Might need to click Refresh again, but your notebook.html will appear in the files, so you can download it:
The great thing about this is that nothing python-related has to be installed on your computer, not conda, not pip, only a browser.
Google Colab doesn't currently have such a feature as a built-in.
Your best route is to first download it through File > Download .ipynb and then use the standard tool for Jupyter Notebook conversion, nbconvert:
jupyter nbconvert --to html notebook.ipynb
If you use an Anaconda Python distribution, nbconvert is most likely already installed. If not, refer to what is described in their install instructions to be able to convert:
pip install nbconvert
# OR
conda install nbconvert
to continue with "Method using only Google Colab"
" %%shell jupyter nbconvert --to html /PATH/TO/YOUR/NOTEBOOKFILE.ipynb" - as given
the following worked for me - type the following in Google Colab
!pip install nbconvert
%shell jupyter nbconvert --to html /content/testfile.ipynb
(instead of using %%shell in Google Colab, use %shell - this way, it worked for me)
Download your notebook and upload it back to the colab
Replace PATH_TO_THE_NOTEBOOK_IN_COLAB in the below command to the location of the reuploaded notebook.
Use this command to download the notebook as html => !jupyter nbconvert --to html PATH_TO_THE_NOTEBOOK_IN_COLAB.ipynb.
I tried the approach above but couldn't get it to work - for small jobs I log onto https://jupyter.org/try , upload the downloaded ipynb file from Google Colab and then I opened the file, from here you will have the functionality in jupyter.org to download as html or whatever other format you require. It took the pain out of trying to get it to work.

How to run an .ipynb Jupyter Notebook from terminal?

I have some code in a .ipynb file and got it to the point where I don't really need the "interactive" feature of IPython Notebook. I would like to just run it straight from a Mac Terminal Command Line.
Basically, if this were just a .py file, I believe I could just do python filename.py from the command line. Is there something similar for a .ipynb file?
nbconvert allows you to run notebooks with the --execute flag:
jupyter nbconvert --execute <notebook>
If you want to run a notebook and produce a new notebook, you can add --to notebook:
jupyter nbconvert --execute --to notebook <notebook>
Or if you want to replace the existing notebook with the new output:
jupyter nbconvert --execute --to notebook --inplace <notebook>
Since that's a really long command, you can use an alias:
alias nbx="jupyter nbconvert --execute --to notebook"
nbx [--inplace] <notebook>
From the command line you can convert a notebook to python with this command:
jupyter nbconvert --to python nb.ipynb
https://github.com/jupyter/nbconvert
You may have to install the python mistune package:
sudo pip install -U mistune
In your Terminal run ipython:
ipython
then locate your script and put there:
%run your_script.ipynb
You can export all your code from .ipynb and save it as a .py script. Then you can run the script in your terminal.
Hope it helps.
Using ipython:
ipython --TerminalIPythonApp.file_to_run=<notebook>.ipynb
Normally, I would prefer this option as it is really self-describing. If you prefer to use less characters, use:
ipython -c "%run <notebook>.ipynb"
which is basically what Keto already suggested (unfortunately a little bit hidden) as a comment.
In my case, the command that best suited me was:
jupyter nbconvert --execute --clear-output <notebook>.ipynb
Why? This command does not create extra files (just like a .py file) and the output of the cells is overwritten everytime the notebook is executed.
If you run:
jupyter nbconvert --help
--clear-output
Clear output of current file and save in place, overwriting the existing notebook.
For new version instead of:
ipython nbconvert --to python <YourNotebook>.ipynb
You can use jupyter instend of ipython:
jupyter nbconvert --to python <YourNotebook>.ipynb
Update with quoted comment by author for better visibility:
Author's note "This project started before Jupyter's execute API, which is now the recommended way to run notebooks from the command-line. Consider runipy deprecated and unmaintained." – Sebastian Palma
Install runipy library that allows running your code on terminal
pip install runipy
After just compiler your code:
runipy <YourNotebookName>.ipynb
You can try cronjob as well. All information is here
I had the same problem and I found papermill. The advantages against the others solutions is that you can see the results while the notebook is running. I find this feature interesting when the notebook takes very long. It is very easy to use:
pip install papermill
papermill notebook.ipynb output.ipynb
It has also, other handy options as saving the output file to Amazon S3, Google Cloud, etc. See the page for more information.
You can also use the boar package to run your notebook within a python code.
from boar.running import run_notebook
outputs = run_notebook("nb.ipynb")
If you update your notebook, you won't have to convert it again to a python file.
More information at:
https://github.com/alexandreCameron/boar/blob/master/USAGE.md
There is now a jupyter run subcommand that will execute a notebook.
jupyter run notebook.ipynb
More on this command can be found in the Jupyter documentation.
From the terminal run
jupyter nbconvert --execute --to notebook --inplace --allow-errors --ExecutePreprocessor.timeout=-1 my_nb.ipynb
The default timeout is 30 seconds. -1 removes the restriction.
If you wish to save the output notebook to a new notebook you can use the flag --output my_new_nb.ipynb
You can also use jupytext https://jupytext.readthedocs.io/en/latest/index.html.
This allows you to pair your notebook. So for each ipynb file you have a .py file as well with some comments. The .py file can be executed as usual.
You can enjoy benefits of two worlds with the cost of one extra file though.
Oh, and by the way if you are using version control you can only commit .py files with a nice diff instead of the ugly .ipynb diffs.
(The syntax in the .py files is similar to Databricks notebooks iy you are familiar with them...)
In a batch file paste the below. Use ipython to run .ipynb files.
#echo on
call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
ipython "path\test.ipynb"
pause
I've made some research on this topic and wrote an article on 4 ways to run Jupyter Notebook in command line below is summary of my findings.
1. Use nbconvert
The nbconvert package is already installed in Jupyter Notebook. It can be used to execute notebook. Additionally it has many features:
can export notebook to PDF or HTML,
can hide code in output notebook,
can execute notebook even with errors in cells.
Example notebook execution:
jupyter nbconvert --execute --to notebook --allow-errors your-notebook.ipynb
The above command will output your-notebook.nbconvert.ipynb file and will execute all cells even with errors.
2. Use papermill
The papermill allows you to parametrize notebook. You can define variables as parameters (with cell tags).
Example command:
papermill -p name Piotrek your-notebook.ipynb output-notebook.ipynb
Example notebook with injected parameters:
3. Manually download notebook as .py script
There is an option to manually download notebook as .py script:
After download you can add execution rights to the file and run it as a command line script.
4. Use jupytext
The jupytext package allows you to synchronize .ipynb file with .py file. You don't need to manually convert notebook to script.

Categories

Resources