Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save oscar6echo/d25a66e29978d4d80c49a6620af67e01 to your computer and use it in GitHub Desktop.

Select an option

Save oscar6echo/d25a66e29978d4d80c49a6620af67e01 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1 - Create a pre run file\n",
"+ Run this section **only once to create/modify the file pre_run.py**\n",
"+ This file is located in the IPython local config folder\n",
"+ It contains Python instructions\n",
"+ It is executed upon IPython kernel launch - before the notebook opens"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/Users/Olivier/.ipython'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = !ipython locate\n",
"dir_ipython = data[0]\n",
"dir_ipython"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/Users/Olivier/.ipython/profile_default/startup/pre_run.py'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"file_prerun = dir_ipython + '/profile_default/startup/pre_run.py'\n",
"file_prerun"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting /Users/Olivier/.ipython/profile_default/startup/pre_run.py\n"
]
}
],
"source": [
"%%writefile {file_prerun}\n",
"\n",
"toto = 22\n",
"\n",
"import pandas as pd\n",
"\n",
"import os\n",
"os.environ['myvar'] = 'myvarvalue'"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r\n",
"toto = 22\r\n",
"\r\n",
"import pandas as pd\r\n",
"\r\n",
"import os\r\n",
"os.environ['myvar'] = 'myvarvalue'"
]
}
],
"source": [
"!cat {file_prerun}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2 - Test pre run file exec\n",
"+ Run this section **right after opening notebook**\n",
"+ To check the impact of pre_run.py on kernel state\n",
"+ You can see that\n",
" + modules were imported\n",
" + variables initialized\n",
" + environment variables set"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"22"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"toto"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'0.20.3'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.__version__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'myvarvalue'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"os.environ.get('myvar')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment