Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. """
  2. Add copy to clipboard from IPython!
  3. To install, just copy it to your profile/startup directory, typically:
  4.  
  5. ~/.ipython/profile_default/startup/
  6.  
  7. Example usage:
  8.  
  9. %copy hello world
  10. # will store "hello world"
  11. a = [1, 2, 3]
  12. %copy a
  13. # will store "[1, 2, 3]"
  14.  
  15. You can also use it with cell magic
  16.  
  17. In [1]: %%copy
  18. ...: Even multi
  19. ...: lines
  20. ...: work!
  21. ...:
  22.  
  23. If you don't have a variable named 'copy' you can rely on automagic:
  24.  
  25. copy hey man
  26. a = [1, 2, 3]
  27. copy a
  28.  
  29. """
  30.  
  31. import subprocess
  32. import sys
  33.  
  34. from IPython.core.magic import register_line_cell_magic
  35.  
  36. def _copy_to_clipboard(output):
  37. output = str(globals().get(output) or output)
  38. process = subprocess.Popen(
  39. 'pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
  40. process.communicate(output.encode('utf-8'))
  41.  
  42. print 'Copied to clipboard!'
  43.  
  44. @register_line_cell_magic
  45. def copy(line, cell=None):
  46. if line and cell:
  47. cell = '\n'.join((line, cell))
  48.  
  49. _copy_to_clipboard(cell or line)
  50.  
  51. # We delete it to avoid name conflicts for automagic to work
  52. del copy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement