Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 Compatibility with In Memory Image Optimization #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
The MIT License (MIT)

Copyright (c) 2014 David Robinson
Copyright (c) 2017 G Gordon

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
author="David Robinson",
author_email="[email protected]",
description="Interactive visualization in Python",
version="0.1.2",
version="0.1.3",
packages=["gleam"],
package_dir={"gleam": "src/gleam"},
package_data={"gleam": [os.path.join("templates", "*"),
Expand Down
2 changes: 1 addition & 1 deletion src/gleam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import os
import json
import urlparse
from collections import namedtuple

from flask import Flask, request, send_from_directory
Expand All @@ -19,6 +18,7 @@ class InputData(object):


class Page(object):

@classmethod
def add_flask(cls, app, path="/"):
"""Add this page to a Flask application at the given path"""
Expand Down
79 changes: 44 additions & 35 deletions src/gleam/panels.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from jinja2 import Environment, PackageLoader
import os
import io
import hashlib
import base64
import random

import wtforms
Expand Down Expand Up @@ -43,10 +45,10 @@ def form_class(self):
class InputForm(Form):
extra = wtforms.fields.HiddenField()

# get the fields
for name, obj in self.__class__.__dict__.iteritems():
if isinstance(obj, UnboundField):
setattr(InputForm, name, obj)
# get the fiel
for name in self.__class__.__dict__:
if isinstance(getattr(self,name),UnboundField):
setattr(InputForm,name,getattr(self,name))

return InputForm

Expand All @@ -73,7 +75,7 @@ class PlotPanel(Panel):
template_name = "plot.html"
width = 700
height = 500
plotter = "ggplot"
plotter = "matplotlib"
extension = "png"
name = "plot"

Expand All @@ -82,9 +84,9 @@ def __init__(self):
self.values["height"] = self.height
self.values["width"] = self.width
self.values["name"] = self.name
self.plot_dir = os.path.join("static", "figures", self.name)
if not os.path.exists(self.plot_dir):
os.makedirs(self.plot_dir)
#self.plot_dir = os.path.join("static", "figures", self.name)
#if not os.path.exists(self.plot_dir):
# os.makedirs(self.plot_dir)

def __call__(self, func):
"""Using as a decorator"""
Expand All @@ -94,35 +96,42 @@ def __call__(self, func):

def refresh(self, data):
"""Generate a new image, then tell the page to change the src"""
h = hashlib.md5(str(data.__dict__)).hexdigest()
print h

outfile = os.path.join(self.plot_dir, h + "." + self.extension)

if not os.path.exists(outfile):

if self.plotter == "custom":
kwargs["__outfile"] = outfile

if self.plotter == "matplotlib":
from matplotlib import pyplot as plt
plt.clf()

ret = self.plot(data)
if ret is None:
# don't change the plot at all
return {self.name: {}}

# after
if self.plotter == "matplotlib":
plt.savefig(outfile)
elif self.plotter == "ggplot":
from ggplot.utils import ggsave
ggsave(outfile, ret)
#cchane

if self.plotter == "custom":
kwargs["__outfile"] = outfile

if self.plotter == "matplotlib":
from matplotlib import pyplot as plt
plt.clf()

ret = self.plot(data)
if ret is None:
# don't change the plot at all
return {self.name: {}}

# after
tempFile = io.BytesIO()

if self.plotter == "matplotlib":

plt.savefig(tempFile,format="png")


elif self.plotter == "ggplot":
from ggplot.utils import ggsave

#ggsave(tempFile, ret)
ret.save(tempFile)
tempFile.seek(0)
base64encodedimage = base64.b64encode(tempFile.getvalue()).decode('utf8')

# turn into a URL, add a dummy param to avoid browser caching
url = outfile.replace(os.path.sep, "/")
url = url + "?dummy=" + str(random.random())
tempFile.seek(0)
base64encodedimage = base64.b64encode(tempFile.getvalue()).decode('utf8')
tempFile.close()

url = 'data:image/png;base64,'+ base64encodedimage

return {self.name: {"src": url}}

Expand Down