Browse Source

Fix cabaret and pygal_gen

pull/58/head
Florian Mounier 12 years ago
parent
commit
953aa19df8
  1. 5
      demo/cabaret.py
  2. 4
      demo/cabaret/__init__.py
  3. 4
      demo/cabaret/static/js.js
  4. 6
      demo/cabaret/templates/_layout.jinja2
  5. 6
      demo/cabaret/templates/index.jinja2
  6. 20
      pygal/config.py
  7. 36
      pygal_gen.py

5
demo/cabaret.py

@ -51,10 +51,11 @@ else:
app.logger.debug('HTTPServer monkey patched for url %s' % url) app.logger.debug('HTTPServer monkey patched for url %s' % url)
try: try:
import wdb from wdb.ext import WdbMiddleware, add_w_builtin
except ImportError: except ImportError:
pass pass
else: else:
app.wsgi_app = wdb.Wdb(app.wsgi_app, start_disabled=True) add_w_builtin()
app.wsgi_app = WdbMiddleware(app.wsgi_app, start_disabled=True)
app.run(debug=True, threaded=True, host='0.0.0.0', port=12221) app.run(debug=True, threaded=True, host='0.0.0.0', port=12221)

4
demo/cabaret/__init__.py

@ -20,7 +20,7 @@ from flask import Flask, render_template, request, jsonify
from pygal import CHARTS_BY_NAME from pygal import CHARTS_BY_NAME
from pygal.graph import CHARTS_NAMES from pygal.graph import CHARTS_NAMES
from pygal.config import CONFIG_ITEMS from pygal.config import CONFIG_ITEMS
from pygal.interpolate import KINDS from pygal.interpolate import INTERPOLATIONS
from pygal.style import styles from pygal.style import styles
from json import loads from json import loads
from time import time from time import time
@ -35,7 +35,7 @@ def create_app():
def index(): def index():
return render_template( return render_template(
'index.jinja2', charts_names=CHARTS_NAMES, configs=CONFIG_ITEMS, 'index.jinja2', charts_names=CHARTS_NAMES, configs=CONFIG_ITEMS,
interpolations=KINDS, styles_names=styles.keys()) interpolations=INTERPOLATIONS, styles_names=styles.keys())
@app.route("/svg", methods=('POST',)) @app.route("/svg", methods=('POST',))
def svg(): def svg():

4
demo/cabaret/static/js.js

@ -15,13 +15,13 @@ function resend() {
} }
}); });
if(interpolation) { if(interpolation) {
opts['interpolate'] = interpolation; opts.interpolate = interpolation;
} }
$('.data .controls').each(function () { $('.data .controls').each(function () {
var label = $(this).find('.serie-label').val(), var label = $(this).find('.serie-label').val(),
values = $(this).find('.serie-value').val(), values = $(this).find('.serie-value').val(),
lst = [label, values.split(',').map(function (v) { return parseFloat(v); })]; lst = [label, values.split(',').map(function (v) { return parseFloat(v); })];
if (values != "") { if (values !== '') {
vals.push(lst); vals.push(lst);
} }
}); });

6
demo/cabaret/templates/_layout.jinja2

@ -2,10 +2,10 @@
<html> <html>
<head> <head>
<title>Cabaret - Online pygal graph generator</title> <title>Cabaret - Online pygal graph generator</title>
<script type="text/javascript" src="{{ url_for('static', filename='components/jquery/jquery.js') }}"></script> <script type="text/javascript" src="{{ url_for('static', filename='components/jquery/jquery.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='components/bootstrap/docs/assets/js/bootstrap.min.js') }}"></script> <script type="text/javascript" src="{{ url_for('static', filename='components/bootstrap/docs/assets/js/bootstrap.min.js') }}"></script>
<script type="text/javascript" src="https://raw.github.com/Kozea/pygal.js/master/svg.jquery.js"></script> <script type="text/javascript" src="http://kozea.github.com/pygal.js/javascripts/svg.jquery.js"></script>
<script type="text/javascript" src="https://raw.github.com/Kozea/pygal.js/master/pygal-tooltips.js"></script> <script type="text/javascript" src="http://kozea.github.com/pygal.js/javascripts/pygal-tooltips.js"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js.js') }}"></script> <script type="text/javascript" src="{{ url_for('static', filename='js.js') }}"></script>
<link rel="stylesheet" href="{{ url_for('static', filename='components/bootstrap/docs/assets/css/bootstrap.css') }}" type="text/css" /> <link rel="stylesheet" href="{{ url_for('static', filename='components/bootstrap/docs/assets/css/bootstrap.css') }}" type="text/css" />
<link rel="stylesheet" href="{{ url_for('static', filename='css.css') }}" type="text/css" /> <link rel="stylesheet" href="{{ url_for('static', filename='css.css') }}" type="text/css" />

6
demo/cabaret/templates/index.jinja2

@ -22,7 +22,7 @@
</select> </select>
</div> </div>
</div> </div>
<div class="control-group data tt" title="Enter the chart data"> <div class="control-group data tt" title="Enter the chart data">
<label class="control-label" for="data">Data</label> <label class="control-label" for="data">Data</label>
<div class="controls form-inline"> <div class="controls form-inline">
@ -71,12 +71,14 @@
</div> </div>
{% else %} {% else %}
<label class="control-label" for="c-{{ key.name }}" > {{ key.name.replace('_', ' ') | title }}</label> <label class="control-label" for="c-{{ key.name }}" > {{ key.name.replace('_', ' ') | title }}</label>
<div class="controls"> <div class="controls">
{% if key.is_numeric %} {% if key.is_numeric %}
<input type="number" id="c-{{ key.name }}" class="c-opts" value="{{ key.value or ''}}" /> <input type="number" id="c-{{ key.name }}" class="c-opts" value="{{ key.value or ''}}" />
{% elif key.is_string %} {% elif key.is_string %}
<input type="text" id="c-{{ key.name }}" class="c-opts" value="{{ key.value or ''}}" /> <input type="text" id="c-{{ key.name }}" class="c-opts" value="{{ key.value or ''}}" />
{% elif key.is_dict %}
<input type="text" id="c-{{ key.name }}" class="c-opts dict-of-{{ key.subtype }}" value="{{ key.value or ''}}" placeholder="key1: value1, key2: value2, ..." />
{% elif key.is_list %} {% elif key.is_list %}
<input type="text" id="c-{{ key.name }}" class="c-opts list-of-{{ key.subtype }}" placeholder="value1, value2{{ ', ...' if key.name != 'range' }}" value="{{ key.value or ''}}" /> <input type="text" id="c-{{ key.name }}" class="c-opts list-of-{{ key.subtype }}" placeholder="value1, value2{{ ', ...' if key.name != 'range' }}" value="{{ key.value or ''}}" />
{% endif %} {% endif %}

20
pygal/config.py

@ -22,6 +22,7 @@ Config module with all options
""" """
from copy import deepcopy from copy import deepcopy
from pygal.style import Style, DefaultStyle from pygal.style import Style, DefaultStyle
from pygal.interpolate import INTERPOLATIONS
class FontSizes(object): class FontSizes(object):
@ -62,6 +63,10 @@ class Key(object):
def is_string(self): def is_string(self):
return self.type == str return self.type == str
@property
def is_dict(self):
return self.type == dict
@property @property
def is_list(self): def is_list(self):
return self.type == list return self.type == list
@ -74,6 +79,17 @@ class Key(object):
map( map(
self.subtype, map( self.subtype, map(
lambda x: x.strip(), value.split(',')))) lambda x: x.strip(), value.split(','))))
elif self.type == dict:
rv = {}
for pair in value.split(','):
key, val = pair.split(':')
key = key.strip()
val = val.strip()
try:
rv[key] = self.subtype(val)
except:
rv[key] = val
return rv
return self.type(value) return self.type(value)
@ -202,7 +218,7 @@ class Config(object):
interpolate = Key( interpolate = Key(
None, str, "Value", "Interpolation", None, str, "Value", "Interpolation",
"May be 'quadratic' or 'cubic'") "May be %s" % ' or '.join(INTERPOLATIONS))
interpolation_precision = Key( interpolation_precision = Key(
250, int, "Value", "Number of interpolated points between two values") 250, int, "Value", "Number of interpolated points between two values")
@ -210,7 +226,7 @@ class Config(object):
interpolation_parameters = Key( interpolation_parameters = Key(
{}, dict, "Value", "Various parameters for parametric interpolations", {}, dict, "Value", "Various parameters for parametric interpolations",
"ie: For hermite interpolation, you can set the cardinal tension with" "ie: For hermite interpolation, you can set the cardinal tension with"
"{'type': 'cardinal', 'c': .5}") "{'type': 'cardinal', 'c': .5}", int)
order_min = Key( order_min = Key(
None, int, "Value", "Minimum order of scale, defaults to None") None, int, "Value", "Minimum order of scale, defaults to None")

36
pygal_gen.py

@ -35,23 +35,25 @@ parser.add_argument('-o', '--output', dest='filename', default='pygal_out.svg',
parser.add_argument('-s', '--serie', dest='series', nargs='+', action='append', parser.add_argument('-s', '--serie', dest='series', nargs='+', action='append',
help='Add a serie in the form (title val1 val2...)') help='Add a serie in the form (title val1 val2...)')
for key, val in pygal.config.Config.__dict__.items(): for key in pygal.config.CONFIG_ITEMS:
if not key.startswith('_') and not hasattr(val, '__call__'): opt_name = key.name
opt_name = key val = key.value
opts = {'type': str} opts = {}
if val is not None: if key.type == list:
opts['type'] = type(val) opts['type'] = key.subtype
elif 'labels' in key: opts['nargs'] = '+'
opts['nargs'] = '+' else:
if opts['type'] == bool: opts['type'] = key.type
del opts['type']
opts['action'] = 'store_true' if not val else 'store_false' if opts['type'] == bool:
if val: del opts['type']
opt_name = 'no-' + opt_name opts['action'] = 'store_true' if not val else 'store_false'
if key == 'interpolate': if val:
opts['choices'] = ['quadratic', 'cubic'] opt_name = 'no-' + opt_name
parser.add_argument( if key.name == 'interpolate':
'--%s' % opt_name, dest=key, default=val, **opts) opts['choices'] = list(pygal.interpolate.INTERPOLATIONS.keys())
parser.add_argument(
'--%s' % opt_name, dest=key.name, default=val, **opts)
config = parser.parse_args() config = parser.parse_args()

Loading…
Cancel
Save