Skip to content

Instantly share code, notes, and snippets.

@Carreau
Created November 11, 2015 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Carreau/02e754e4948efdccf048 to your computer and use it in GitHub Desktop.
Save Carreau/02e754e4948efdccf048 to your computer and use it in GitHub Desktop.
Response to StackOverflow 33508377
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Response on StackOverflow:\n",
"\n",
"http://stackoverflow.com/questions/33508377/read-cell-content-in-an-ipython-notebook"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from IPython.core.magic import Magics"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from IPython.core.magic import (\n",
" Magics, magics_class, cell_magic, line_magic\n",
")\n",
"\n",
"class B(dict):\n",
" def __getattr__(self, name):\n",
" return self.__getitem__(name)\n",
"\n",
"@magics_class\n",
"class StoreSQL(Magics):\n",
"\n",
" \n",
" def __init__(self, shell=None, **kwargs):\n",
" super().__init__(shell=shell, **kwargs)\n",
" self._store = []\n",
" self._store2 = B()\n",
" # inject our store in user availlable namespace under __mystore\n",
" # name\n",
" shell.user_ns['__mystore'] = self._store\n",
" shell.user_ns['__mystore2'] = self._store2\n",
"\n",
" @cell_magic\n",
" def sql(self, line, cell):\n",
" \"\"\"store the cell in the store\"\"\"\n",
" self._store.append(cell)\n",
"\n",
" @cell_magic\n",
" def sqlnamed(self, line, cell):\n",
" \"\"\"store the cell in the store\"\"\"\n",
" self._store2[line.strip()] = cell\n",
"\n",
" \n",
" @line_magic\n",
" def showsql(self, line):\n",
" \"\"\"show all recorded statements\"\"\"\n",
" print(self._store)\n",
" print(self._store2)\n",
" \n",
"## use ipython load_ext mechanisme here if distributed\n",
"get_ipython().register_magics(StoreSQL)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"b = B()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"b['A'] = 1"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b.A"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%%sql \n",
"select * from foo Where QUX Bar"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%%sql\n",
"Insert Cheezburger into Can_I_HAZ"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['select * from foo Where QUX Bar', 'Insert Cheezburger into Can_I_HAZ']\n",
"{}\n"
]
}
],
"source": [
"%showsql"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['select * from foo Where QUX Bar', 'Insert Cheezburger into Can_I_HAZ']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"__mystore"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%%sqlnamed X1\n",
"this is sql2 cell X1"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%%sqlnamed thisIsFun\n",
"this is sql2 cell Fun"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['select * from foo Where QUX Bar', 'Insert Cheezburger into Can_I_HAZ']\n",
"{'X1': 'this is sql2 cell X1', 'thisIsFun': 'this is sql2 cell Fun'}\n"
]
}
],
"source": [
"%showsql"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'this is sql2 cell X1'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"__mystore2.X1"
]
}
],
"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.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment