Ipython Notebook: Default Initialization Python Code - jupyter-notebook

I use iPython Notebook for scientific applications and I'm always plotting experimental data sets. The default image rendering and color cycling behavior of the combination of iPython Notebook and Matplotlib is pretty bad, but it's great after the following tweaks.
# ipython notebook specific
%pylab inline
# imports
from matplotlib import pyplot as plt
import matplotlib as mpl
from seaborn.apionly import set_palette
from IPython.display import set_matplotlib_formats
# configure plotting
set_matplotlib_formats('pdf', 'svg')
set_palette('Set1', n_colors=15, desat=None)
mpl.rcParams['figure.figsize']=(12.0,2.0)
I don't want to have to enter these manually in every notebook. How can I have these executed for all notebooks I'll ever create?

You can create Python files in $HOME/.ipython/profile_default/startup/ that contains the code you want executed at startup.
For example, you can create $HOME/.ipython/profile_default/startup/00-init.py:
# imports
from matplotlib import pyplot as plt
import matplotlib as mpl
from seaborn.apionly import set_palette
from IPython.display import set_matplotlib_formats
# configure plotting
set_matplotlib_formats('pdf', 'svg')
set_palette('Set1', n_colors=15, desat=None)
mpl.rcParams['figure.figsize']=(12.0,2.0)
Note that IPython magics are not supported here, so %matplotlib inline won't work. This question deals with making %matplotlib inline the default.
You should be aware that if you change the defaults for your IPython environment, your notebooks may not work on other people's IPython installations.

Related

Running old jupyter notebooks

I am trying to use a Jupyter notebook that was written by somebody else around 8 years ago. And the contents are written in Python2 and not Python3 that I am using. I am new to it and I can't figure out the reason for the syntax error.
The following are the contents of the first cells that is giving a syntax error.
`# Always run this first
# NOTE: Do not define new basic variables in this notebook;
# define them in Variables_Q.ipynb. Use this notebook
# to define new expressions built from those variables.
from __future__ import division # This needs to be here, even though it's in Variables_Q.ipynb
import sys
import os
sys.path.append(os.path.join(os.path.dirname('/home/khushal/Desktop/Python/Git Repositories/PostNewtonian/PNTerms'
)) # Look for modules in directory above this one
exec(open('../Utilities/ExecNotebook.ipy').read(),{})
from ExecNotebook.ipy import execnotebook
try: execnotebook(VariablesNotebook)
except: execnotebook('Variables_Q.ipynb')`
The error is
File "/tmp/ipykernel_31372/53020713.py", line 11
exec(open('../Utilities/ExecNotebook.ipy').read(),{})
^
SyntaxError: invalid syntax
I tried to use a 2to3 jupter notebook converter but it didn't change the jupyter notebook at all and it is giving the same result.

How to import python libraries every time Jupyter interactive window starts in VS Code

Whenever I start the Jupyter Interactive window in VS Code (using, ctrl+shift+p), I have to load several libraries that I use like numpy and pandas. Is there any way in which a selected set of libraries could be made to auto import every time the Jupyter Interactive window starts
something like
import numpy as np
import pandas as pd
every time a new interactive window starts

from import from python in reticulate

Any way to do the from - import from Python in Reticulate without having to import the whole module?
example: from tensorflow import keras
I know about the solution below, but it imports the whole module anyway...
keras <- import('tensorflow')$keras
I am not sure, but if you are writing your r code in an rmd file, wouldn't you be able to do it directly in a python chunk like this:
python
from ... import ...

Jupyter Notebook cell - Repeat same imports?

I have multiple Notebook cells where im importing the same libraries.
Is there a way to import these libraries once (like in the very 1st cell) and allow all other cells to use them?
Since my notebook has several cells, each importing the same libraries, i'm starting to see alot of redundancies.
Of course! You can add all your libraries at the very top of your notebook. All of them will be available for use.
import pandas as pd
import numpy as np
import matolotlib.pyplot as plt
import seaborn as sns

Is it possible to use nbconvert to export a Bokeh plot to a single html file?

I tried to use nbconvert > HTML and it returns random bokeh logos
here is my code:
import pandas as pd
from bokeh.plotting import *
output_notebook()
df = pd.DataFrame({'rev':[234,345,657,7]})
df
line(df['rev'],df['rev'], color="red", line_width=2,
title="Archimean", legend="Archimedean")
show()
A small CSS bug made it into 0.5.0 that affects nbconvert output. There is alread a fix checked into master. We will be releasing 0.5.1 early next week.

Resources