Skip to main content

Clipboard utility for ipython

Project description

IPyclip

An IPyhon extension providing similar to windows' clip.exe

Setup

Install using:

pip install ipyclip  

You can now either load the extension manually with the ipython magic:

%load_ext ipyclip  

Or configure IPython to automatically load the extension by adding this to the IPython profile's configuration file:

c.InteractiveShellApp.extensions = [  
    'ipyclip'
]  

The clip object

When loading the extension, you get access to the clip object.
This is an instance of the ClipboardManager class, and it handles output and return value capture for you. Copying is done by calling pyperclip's copy method. clip is configured and injected into the shells user namespace when the extension is loaded.
Currently, only clip is provided out of the box, and the only stream it supports is sys.stdout.
The ClipboardManager class does support other stream objects in theory, but they weren't tested.

Usage

You interact with the clip object by "redirecting" the output of a statement into the object, like with shell redirection.

Copy Modes

  • > - Stream output.
  • >> - Stream output, add to last copied item.
  • / - return value, add to last copied item.
  • // - return value, add to last copied item.
  • | - auto (both stream and return value)

You can always copy the last execution's output by invoking:

copy()  

[!NOTE] Copying behavior. pyperclip.copy first turns the object it receives into a string by invoking its str(object), this means that objects lacking the __str__ method or raise an unhandled exception when __str__ is called will not be copied. This also means that when copying special characters (like \n) will behave as you would expect those characters to behave as part of the string that they're in. Ex:

"Hello\nworld!"
# Would be pasted like:
# Hello
# World

r"Hello\nworld"
# Would be pasted like:
# Hello\nworld

Stream Output

Override - >

{some_expression} > clip

Copies anything written to the stream during execution as is.

print("Some data") > clip  
# in clipboard: 'Some data\n'  
  
print("Some data", end="") > clip  
# in clipboard: "Some data"  
  
for i in range(5):  
    print(i) > clip  
  
# in clipboard:  
# "0\n1\n2\n3\n4\n"  

Concatenate - >>

{some_expression} >> clip

Adds the stream output to what was previously copied to the clipboard.

# last clip: ""
print("Some data") >> clip  
# in clipboard: Some data\n

# last clip: "Some data\n"
print("Other data", end="") > clip  
# in clipboard: Some data\nOther data
 
# last clip: Some data\nOther data
for i in range(5):  
   print(i) > clip  

# in clipboard: Some data\nOther data0\n1\n2\n3\n4\n

Return Value

Override - /

{some_expression} / clip

Copy the return value of the expression on the left. Stream output is ignored.

def ex1():
	return "Hello"

def ex2():
	return 1

def ex3():
	print("Hello")
	return "World"

def ex4():
	print("Example")
	# Return value is None	

ex1 / clip # In clipboard: Hello
ex2 / clip # In clipboard: 1
ex3 / clip # In clipboard: world
ex4 / clip # In clipboard: None

Concatenate - //

{some_expression} // clip

Adds the return value of the expression on the left to what was previously copied to the clipboard. Adds a line break to help separate the values.

def ex1():
	return "Hello"

def ex2():
	return 1

def ex3():
	print("Hello")
	return "World"

def ex4():
	print("Example")
	# Return value is None	

ex1 // clip # In clipboard: Hello
ex2 // clip # In clipboard: Hello\n1
ex3 // clip # In clipboard: Hello\n1world
ex4 // clip # In clipboard: Hello\n1world\nNone

Auto mode - |

{some_statement} | clip  

Copies both the stream output and the return value. Adds a line break between the stream output and return value if the stream output doesn't end with \n already.

print("Hello World") | clip # In clipboard: Hello World\nNone



def ex3():
	print("Hello")
	return "World"

def ex4():
	print("Example")
	# Return value is None	

ex1  clip # In clipboard: Hello
ex2 // clip # In clipboard: Hello\n1
ex3 // clip # In clipboard: Hello\n1world
ex4 // clip # In clipboard: Hello\n1world\nNone

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ipyclip-0.2.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file ipyclip-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: ipyclip-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for ipyclip-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9660537db2dc2b2e3f285e2da18e073f3b63e5a295050c2797751ae05e78a46d
MD5 31d339a1b2e1ca7fea53691ea3d31a93
BLAKE2b-256 50fc69ec418b7e37dc9e852d368822d240b6ab93cb6ae6d470a2e58464959c4f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page