def __getattr__(self, view): if view.startswith('_'): # This object is not an iterator but it sometimes gets asked this # way so since we're providing a catch-all attribute handler we # want to make sure we don't mislead the caller into thinking # we have an __iter__ method. While we're at it we should make # sure we're not misleading about anything starting with '_'. raise AttributeError('{!r} object has no attribute {!r}'.format( self.__class__.__name__, view)) try: return getattr(self.model, view) except AttributeError as e: component(css=MISSING_CSS) return \ self.render(view) or \ MISSING.format('.'.join([self.__class__.__name__, view]))
def timeline(data, options=None, **kwargs): """produce a gantt timeline chart""" chart_id = get_chart_id(**kwargs) title = kwargs.pop('title', None) title = title and '<div class="visjs-title">{}</div>'.format(title) or '' default_options = {} options = merge_options(merge_options(default_options, options), kwargs) content = """ %s <div class="dz-visjs-chart placeholder" id="%s"></div> """ % (title, chart_id) js = """ $(function(){ var container = document.getElementById("%(selector)s"); var chart = new vis.DataSet( %(data)s ); var options = %(options)s; var timeline = new vis.Timeline(container, chart, options); // make the object accessible later $("#%(selector)s").data('visjs-chart', chart); }); """ % dict(selector=chart_id, data=data, options=json.dumps(options, indent=4)) return component(content, js=js, libs=libs, styles=styles, css=css)
def index(self): """produce the index page for the app""" content = component( '<a class="hello-link" href="view-example/hello">Say Hello!</a>', css='.hello-link { color: red; }', ) return page(content, title='Hello')
def donut(data, options=None, **kwargs): """produce a donut chart""" chart_id = get_chart_id(**kwargs) default_options = { 'data': { 'columns': data, 'type': 'donut', }, 'tooltip': { 'format': { 'value': '<<formatter>>', }, }, 'bindto': '#' + chart_id, } options = merge_options(merge_options(default_options, options), kwargs) content = """ <div class="dz-c3-chart placeholder" id="{}"></div> """.format(chart_id) formatter = 'function(x){return x}' options = json.dumps(options, indent=4).replace( '"<<formatter>>"', formatter) js = """ $(function(){ var chart = c3.generate(%(options)s); }); """ % dict(options=options) return component(content, js=js, libs=libs, styles=styles, css=css)
def render(self): return component( self.html(), css=self.css(), libs=self.scripts, styles=self.styles )
def index(self): footer = '{} records'.format(len(self.data)) content = component( browse(self.data, title="Browse", footer=footer), #browse(self.data, title="Table", footer=footer), ) return page(content, title='Tables')
def __getattr__(self, view): if view.startswith('_'): # This object is not an iterator but it sometimes gets asked this # way so since we're providing a catch-all attribute handler we # want to make sure we don't mislead the caller into thinking # we have an __iter__ method. While we're at it we should make # sure we're not misleading about anything starting with '_'. raise AttributeError('{!r} object has no attribute {!r}'.format( self.__class__.__name__, view )) try: return getattr(self.model, view) except AttributeError, e: component(css=MISSING_CSS) return \ self.render(view) or \ MISSING.format('.'.join([self.__class__.__name__, view]))
def line(data, legend=None, options=None, **kwargs): """produce a line chart""" # pylint: disable=star-args # It's reasonable in this case. chart_id = kwargs.pop('chart_id', 'chart_' + uuid.uuid4().hex) data = zip(*data) default_options = { 'legend': { 'position': 'inset' }, } options = merge_options(merge_options(default_options, options), kwargs) #legend = options.get('legend', None) labels = data[0] rows = [] rows.append(['x'] + list(labels)) if legend: if len(legend) != len(data) - 1: msg = '{} legend must match number of data columns' raise Exception(msg.format(__file__)) for n, label in enumerate(legend): rows.append([label] + list(data[n + 1])) content = """ <div class="dz-c3-chart placeholder" id="%s"></div> """ % chart_id js = """ $(function(){ var chart = c3.generate({ bindto: '#%(chart_id)s', title: {text: '%(title)s'}, data: { x: 'x', columns: %(data)s }, axis: { x: { type: 'category' } } }); }); """ % dict( chart_id=chart_id, data=json.dumps(rows), title=kwargs.get('title', ''), ) return component(content, js=js, libs=libs, styles=styles, css=css)
def bar(data, options=None, **kwargs): """produce a line chart""" chart_id = get_chart_id(**kwargs) data = zip(*data) legend = kwargs.pop('legend', None) labels = data[0] rows = [] rows.append(['x'] + list(labels)) if legend: if len(legend) != len(data) - 1: msg = '{} legend must match number of data columns' raise Exception(msg.format(__file__)) for n, label in enumerate(legend): rows.append([label] + list(data[n + 1])) default_options = { 'data': { 'x': 'x', 'columns': rows, 'type': 'bar' }, 'legend': { 'position': 'inset' }, 'bar': { 'width': { 'ratio': 0.5 } }, 'axis': { 'x': { 'type': 'category' } }, 'legend': { 'position': 'inset' }, 'bindto': '#' + chart_id } options = merge_options(merge_options(default_options, options), kwargs) content = """ <div class="dz-c3-chart placeholder" id="{}"></div> """.format(chart_id) js = """ $(function(){ var chart = c3.generate(%(options)s); }); """ % dict(options=json.dumps(options, indent=4)) return component(content, js=js, libs=libs, styles=styles, css=css)
def render(self, view=None): assets = self.get_assets(view) if assets: result = {} for k, v in assets.items(): if k in ['html']: result[k] = v.format(self=self) elif k in ['js']: result[k] = self.js_wrapper.format(v) else: result[k] = v return component(**result)
def gauge(data, label=None, intervals=None, interval_colors=None, options=None, **kwargs): """produce a gauge chart""" chart_id = get_chart_id(**kwargs) min_value = kwargs.pop('min', 0) max_value = kwargs.pop('max', 100) default_options = { 'data': { 'columns': [['', data]], 'type': 'gauge', }, 'gauge': { 'units': label, 'min': min_value, 'max': max_value, 'label': { 'show': True, 'format': '<<formatter>>', }, }, 'color': { 'pattern': interval_colors, 'threshold': { 'values': intervals } }, 'bindto': '#' + chart_id, } options = default_options content = """ <div class="dz-c3-chart placeholder" id="{}"></div> """.format(chart_id) formatter = 'function(value, ratio) { return value; }' options = json.dumps(options, indent=4).replace('"<<formatter>>"', formatter) js = """ $(function(){ var chart = c3.generate(%(options)s); }); """ % dict(options=options) return component(content, js=js, libs=libs, styles=styles, css=css)
def line(data, options=None, **kwargs): """produce a line chart""" # pylint: disable=star-args # It's reasonable in this case. chart_id = get_chart_id(**kwargs) legend = kwargs.pop('legend', None) data = zip(*data) labels = data[0] rows = [] rows.append(['x'] + list(labels)) if legend: if len(legend) != len(data) - 1: msg = '{} legend must match number of data columns' raise Exception(msg.format(__file__)) for n, label in enumerate(legend): rows.append([label] + list(data[n + 1])) default_options = { 'data': { 'x': 'x', 'columns': rows, }, 'legend': { 'position': 'inset', }, 'axis': { 'x': { 'type': 'category' }, }, 'bindto': '#' + chart_id } options = merge_options(merge_options(default_options, options), kwargs) content = """ <div class="dz-c3-chart placeholder" id="%s"></div> """ % chart_id js = """ $(function(){ var chart = c3.generate(%(options)s); // make the object accessible later $("#%(selector)s").data('c3-chart', chart); }); """ % dict(selector=chart_id, options=json.dumps(options, indent=4)) return component(content, js=js, libs=libs, styles=styles, css=css)
def callback(method, url=None, timeout=5000): method_name = method.__name__ path = url or '/<dz:app_name>/' + method_name js = """ jQuery(function($){ setInterval(function(){ $.get('%(path)s', function( content ){ if (content) { $('#%(method_name)s').html( content ); } }); }, %(timeout)s); }); """ % dict(path=path, method_name=method_name, timeout=timeout) content = '<div id="%(method_name)s">%(initial_value)s</div>' % dict( initial_value=method(), method_name=method_name) return component(content, js=js)
def chart(selector, data, options): return component( '<span class="{} inojs">{}</span>'.format(selector, repr(data)[1:-1]), js=js.replace('<<options>>', repr(options)), libs=libs )
def index(self): footer = '{} records'.format(len(self.data)) content = component(browse(self.data, title="Browse", footer=footer), #browse(self.data, title="Table", footer=footer), ) return page(content, title='Tables')
def render(self): return component(self.html(), css=self.css(), libs=self.scripts, styles=self.styles)
def __repr__(self): return self.render() or component( MISSING.format(self.__class__.__name__), css=MISSING_CSS )
def __repr__(self): return self.render() or component( MISSING.format(self.__class__.__name__), css=MISSING_CSS)
def chart(selector, data, options): return component('<span class="{} inojs">{}</span>'.format( selector, repr(data)[1:-1]), js=js.replace('<<options>>', repr(options)), libs=libs)