Skip to content

Instantly share code, notes, and snippets.

@whiledoing
Last active January 22, 2020 09:28
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 whiledoing/71146ddba034511aaaa71a0646b3c75f to your computer and use it in GitHub Desktop.
Save whiledoing/71146ddba034511aaaa71a0646b3c75f to your computer and use it in GitHub Desktop.
[ipython-notebook] ipython related #python
"""
Add copy to clipboard from IPython!
To install, just copy it to your profile/startup directory, typically:
~/.ipython/profile_default/startup/
Example usage:
%clip hello world
# will store "hello world"
a = [1, 2, 3]
%clip a
# will store "[1, 2, 3]"
%clip_eval pd.to_dict()
You can also use it with cell magic
In [1]: %%clip
...: Even multi
...: lines
...: work!
...:
If you don't have a variable named 'clip' you can rely on automagic:
clip hey man
a = [1, 2, 3]
clip a
"""
import subprocess
import sys
from IPython.core.magic import register_line_cell_magic
def _copy_to_clipboard(output, use_eval=False):
if use_eval:
try:
output = repr(eval(output))
except Exception as e:
print(e)
return
else:
output = str(globals().get(output) or output)
process = subprocess.Popen(
'pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
process.communicate(output.encode('utf-8'))
print('Copied to clipboard!')
@register_line_cell_magic
def clip_eval(line, cell=None):
if line and cell:
cell = '\n'.join((line, cell))
_copy_to_clipboard(cell or line, use_eval=True)
@register_line_cell_magic
def clip(line, cell=None):
if line and cell:
cell = '\n'.join((line, cell))
_copy_to_clipboard(cell or line, use_eval=False)
# We delete it to avoid name conflicts for automagic to work
del clip
import sys
from IPython.core.debugger import Pdb
def set_trace():
Pdb().set_trace(sys._getframe().f_back)
def debug(f, *args, **argv):
return Pdb().runcall(f, *args, **argv)
%%snakeviz
import glob
files = glob.glob('*.txt')
for file in files:
with open(file) as f:
print(hashlib.md5(f.read().encode('utf-8')).hexdigest())
import os
import sys
import matplotlib as mpl
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# change default rcParam
plt.rcParams['savefig.dpi'] = 200
plt.rcParams['figure.dpi'] = 150
# Pandas options
pd.options.display.max_columns = 30
pd.options.display.max_rows = 20
from IPython import get_ipython
ipython = get_ipython()
# If in ipython, load autoreload extension
if 'ipython' in globals():
print('\nWelcome to IPython!')
ipython.magic('load_ext snakeviz')
ipython.magic('load_ext autoreload')
ipython.magic('autoreload 2')
ipython.magic("config InlineBackend.figure_format = 'retina'")
# Display all cell outputs in notebook
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'
# debug
def alert():
from IPython.display import Javascript
from IPython.display import display
js = """alert("All done!");"""
display(Javascript(js))
def set_trace():
from IPython.core.debugger import Pdb
Pdb().set_trace(sys._getframe().f_back)
def debug(f, *args, **argv):
from IPython.core.debugger import Pdb
return Pdb().runcall(f, *args, **argv)
import seaborn as sns
sns.set()
print('whiledonig - Your favorite libraries have been loaded.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment