Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Initial commit to GitHub
  • Loading branch information
Joshua Burnett committed Nov 27, 2018
0 parents commit df4b54c
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
*.egg-info
__pycache__
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include README.md
23 changes: 23 additions & 0 deletions README.md
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
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty on purpose
59 changes: 59 additions & 0 deletions addcopyfighandler.py
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
59 changes: 59 additions & 0 deletions build/lib/addcopyfighandler.py
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 added dist/addcopyfighandler-1.0.1-py2.py3-none-any.whl
Binary file not shown.
Binary file added dist/addcopyfighandler-1.0.1.tar.gz
Binary file not shown.
1 change: 1 addition & 0 deletions generate_source_dist.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python setup.py sdist --formats=gztar,zip bdist_wheel
55 changes: 55 additions & 0 deletions setup.py
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",
)

0 comments on commit df4b54c

Please sign in to comment.