You can use the magic function %run <filename>
where you've got your utility functions defined.
I have had great use for this when I'm writing new utility functions and want to test and improve them in a jupyter notebook without restarting the whole thing each time. Check out chapter IPython Magic Commands by Jake VanderPlas for
Details:
I've got a file named utils.py
in a folder named c:/jupytertest
.
Utils.py:
import pandas as pd
def framer(df):
df_out = df['parch'].to_frame()
return(df_out)
And I've got the following in a jupyter notebook cell:
# standard imports
import os
import pandas as pd
os.chdir('C:/jupytertest/')
# run / reload utils
%run utils.py
# data
url = "https://raw.github.com/mattdelhey/kaggle-titanic/master/Data/train.csv"
titanic = pd.read_csv(url)
# test reloading of function from utils.py
values = framer(titanic)
print(values.tail(5))
Now every time I change df_out = df['parch'].to_frame()
to name
or any other column I know exists in the titanic dataset, the only thing I have to do to apply the up-dated function in jupyter is to save utils.py
and re-run the cell in the notebook.
No kernel-restart in the notebook should be necessary.
Check out IPython Magic Commands in the Python Data science Handbook by Jake VanderPlas for even more details.