-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit to GitHub
- Loading branch information
Joshua Burnett
committed
Nov 27, 2018
0 parents
commit df4b54c
Showing
10 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
*.egg-info | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
addcopyfighandler: Adds a Ctrl+C handler to matplotlib figures for copying the figure to the clipboard | ||
====================================================================================================== | ||
Simply importing this module (after importing matplotlib) will add a handler | ||
so that pressing Ctrl+C with a matplotlib figure window selected will copy | ||
the figure to the clipboard as an image. | ||
|
||
Original concept code taken from: | ||
https://stackoverflow.com/questions/31607458/how-to-add-clipboard-support-to-matplotlib-figures | ||
|
||
|
||
Releases | ||
-------- | ||
|
||
### 1.0: 2017-08-09 | ||
|
||
- Initial release | ||
|
||
|
||
### 1.0.1: 2018-11-27 | ||
|
||
- Improve setup.py: remove need for importing module, add proper installation dependencies | ||
- Change readme from ReST to Markdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# empty on purpose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Monkey-patch plt.figure() to support Ctrl+C for copying to clipboard as an image | ||
@author: Josh Burnett | ||
Modified from code found on Stack Exchange: | ||
https://stackoverflow.com/questions/31607458/how-to-add-clipboard-support-to-matplotlib-figures | ||
""" | ||
|
||
|
||
import matplotlib.pyplot as plt | ||
from win32gui import GetWindowText, GetForegroundWindow | ||
|
||
import io | ||
import qtpy | ||
|
||
__version__ = (1, 0, 1) | ||
|
||
if qtpy.API == 'pyqt4': | ||
from PyQt4.QtGui import QApplication, QImage | ||
clipboard = QApplication.clipboard | ||
elif qtpy.API == 'pyqt5': | ||
from PyQt5.QtGui import QGuiApplication, QImage | ||
clipboard = QGuiApplication.clipboard | ||
|
||
oldfig = plt.figure | ||
|
||
|
||
def copyfig(fig=None): | ||
# store the image in a buffer using savefig(), this has the | ||
# advantage of applying all the default savefig parameters | ||
# such as background color; those would be ignored if you simply | ||
# grab the canvas using Qt | ||
if fig is None: | ||
# find the figure window that has UI focus right now (not necessarily the same as plt.gcf()) | ||
fig_window_text = GetWindowText(GetForegroundWindow()) | ||
for i in plt.get_fignums(): | ||
if plt.figure(i).canvas.get_window_title() == fig_window_text: | ||
fig = plt.figure(i) | ||
break | ||
|
||
buf = io.BytesIO() | ||
fig.savefig(buf) | ||
clipboard().setImage(QImage.fromData(buf.getvalue())) | ||
buf.close() | ||
|
||
|
||
def newfig(*args, **kwargs): | ||
fig = oldfig(*args, **kwargs) | ||
|
||
def clipboard_handler(event): | ||
if event.key == 'ctrl+c': | ||
copyfig() | ||
|
||
fig.canvas.mpl_connect('key_press_event', clipboard_handler) | ||
return fig | ||
|
||
|
||
plt.figure = newfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Monkey-patch plt.figure() to support Ctrl+C for copying to clipboard as an image | ||
@author: Josh Burnett | ||
Modified from code found on Stack Exchange: | ||
https://stackoverflow.com/questions/31607458/how-to-add-clipboard-support-to-matplotlib-figures | ||
""" | ||
|
||
|
||
import matplotlib.pyplot as plt | ||
from win32gui import GetWindowText, GetForegroundWindow | ||
|
||
import io | ||
import qtpy | ||
|
||
__version__ = (1, 0, 1) | ||
|
||
if qtpy.API == 'pyqt4': | ||
from PyQt4.QtGui import QApplication, QImage | ||
clipboard = QApplication.clipboard | ||
elif qtpy.API == 'pyqt5': | ||
from PyQt5.QtGui import QGuiApplication, QImage | ||
clipboard = QGuiApplication.clipboard | ||
|
||
oldfig = plt.figure | ||
|
||
|
||
def copyfig(fig=None): | ||
# store the image in a buffer using savefig(), this has the | ||
# advantage of applying all the default savefig parameters | ||
# such as background color; those would be ignored if you simply | ||
# grab the canvas using Qt | ||
if fig is None: | ||
# find the figure window that has UI focus right now (not necessarily the same as plt.gcf()) | ||
fig_window_text = GetWindowText(GetForegroundWindow()) | ||
for i in plt.get_fignums(): | ||
if plt.figure(i).canvas.get_window_title() == fig_window_text: | ||
fig = plt.figure(i) | ||
break | ||
|
||
buf = io.BytesIO() | ||
fig.savefig(buf) | ||
clipboard().setImage(QImage.fromData(buf.getvalue())) | ||
buf.close() | ||
|
||
|
||
def newfig(*args, **kwargs): | ||
fig = oldfig(*args, **kwargs) | ||
|
||
def clipboard_handler(event): | ||
if event.key == 'ctrl+c': | ||
copyfig() | ||
|
||
fig.canvas.mpl_connect('key_press_event', clipboard_handler) | ||
return fig | ||
|
||
|
||
plt.figure = newfig |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python setup.py sdist --formats=gztar,zip bdist_wheel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import re | ||
from setuptools import setup | ||
|
||
with open('README.md') as readme: | ||
long_description = readme.read() | ||
|
||
|
||
def get_version(filename='addcopyfighandler.py'): | ||
""" Extract version information stored as a tuple in source code """ | ||
version = '' | ||
with open(filename, 'r') as fp: | ||
for line in fp: | ||
m = re.search('__version__ .* \((.*)\)', line) | ||
if m is not None: | ||
version = m.group(1).replace(', ', '.') | ||
break | ||
return version | ||
|
||
|
||
# What packages are required for this module to be executed? | ||
REQUIRED = [ | ||
'matplotlib', 'pywin32', 'qtpy', | ||
] | ||
|
||
setup( | ||
name = "addcopyfighandler", | ||
version = get_version(), | ||
|
||
py_modules=["addcopyfighandler"], | ||
|
||
install_requires=REQUIRED, | ||
|
||
classifiers=[ | ||
'Development Status :: 4 - Beta', | ||
'Intended Audience :: Developers', | ||
'License :: OSI Approved :: MIT License', | ||
|
||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 2.7', | ||
'Programming Language :: Python :: 3', | ||
'Programming Language :: Python :: 3.6', | ||
'Programming Language :: Python :: 3.7', | ||
], | ||
|
||
# metadata for upload to PyPI | ||
author = "Josh Burnett", | ||
author_email = "[email protected]", | ||
description = "Adds a Ctrl+C handler to matplotlib figures for copying the figure to the clipboard", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
license = "MIT", | ||
keywords = "addcopyfighandler figure matplotlib handler copy", | ||
url = "https://github.com/joshburnett/addcopyfighandler", | ||
platform="windows", | ||
) |