def test_3_wrong_field_name(self): from datatable import DataTable data = DataTable("data.csv") with self.assertRaises(KeyError): next( data.find_n(3, where={ "Year": "2017", "Variable_cod": "H01", }))
def iterBucket(self, *fields): copy = self.sorted(*fields) currentKey = None currentBucket = [] for data in copy: key = tuple(data[field] for field in fields) if currentKey is not None and key != currentKey: yield currentKey, DataTable(currentBucket) currentBucket = [] currentKey = key currentBucket.append(data) yield currentKey, DataTable(currentBucket)
class TestDataTable(unittest.TestCase): '''Test DataTable''' def setUp(self): import db from datatable import DataTable self.db = db.getInstance() self.datatable = DataTable(self.db, 'disciplines') def test_columnDefs(self): expected = [ {'type': 'int(10) unsigned', 'name': 'discipline_id'}, {'type': 'varchar(30)', 'name': 'code'}, {'type': 'varchar(45)', 'name': 'name'}, {'type': 'varchar(255)', 'name': 'description'}, {'type': 'int(10) unsigned', 'name': 'active'}, {'type': 'timestamp', 'name': 'last_updated'}, ] sort = lambda r: sorted(r, key=lambda x: x['name']) results = sort(self.datatable.columnDefs()) actual = sort(results) expected = sort(expected) for old, new in zip(expected, actual): self.assertDictEqual(old, new) def test_table_columns(self): expected = ['discipline_id', 'code', 'name', 'description', 'active', 'last_updated'] results = self.datatable.table_columns self.assertEqual(expected, results) def test_describe(self): sort = lambda r: sorted(r, key=lambda x: x['Field']) expected = sort([ {'Extra': '', 'Default': None, 'Field': 'discipline_id', 'Key': 'PRI', 'Null': 'NO', 'Type': 'int(10) unsigned'}, {'Extra': '', 'Default': None, 'Field': 'code', 'Key': 'MUL', 'Null': 'NO', 'Type': 'varchar(30)'}, {'Extra': '', 'Default': None, 'Field': 'name', 'Key': '', 'Null': 'NO', 'Type': 'varchar(45)'}, {'Extra': '', 'Default': None, 'Field': 'description', 'Key': '', 'Null': 'YES', 'Type': 'varchar(255)'}, {'Extra': '', 'Default': None, 'Field': 'active', 'Key': '', 'Null': 'NO', 'Type': 'int(10) unsigned'}, {'Extra': 'on update CURRENT_TIMESTAMP', 'Default': 'CURRENT_TIMESTAMP', 'Field': 'last_updated', 'Key': '', 'Null': 'NO', 'Type': 'timestamp'} ]) results = sort(self.datatable.describe()) for old, new in zip(expected, results): self.assertDictEqual(old, new)
def result(): if not cur.description: return None headers = [h[0] for h in cur.description] def zipHeaders(row): d = {} for i, h in enumerate(headers): if h not in d or d[h] is None: d[h] = row[i] elif row[i] and row[i] != d[h]: print( 'WARNING: query returned multiple columns with the same name (%s) with conflicting data. Taking the data from the first returned column' % h) return d theData = [zipHeaders(row) for row in cur.fetchall()] if scrub is not None: for desc in cur.description: replace = scrub(desc) if replace is not None: for row in theData: row[desc[0]] = replace(row[desc[0]]) if customScrub: for row in theData: for header, fromDbValue in customScrub.items(): if row[header] is not None: try: row[header] = fromDbValue(row[header]) except: pass if indexedResults: return Hierarchy.fromTable(theData, index, set(headers).difference(index)) return DataTable(theData)
class TestDataTable(unittest.TestCase): '''Test DataTable''' def setUp(self): import db from datatable import DataTable self.db = db.getInstance() self.datatable = DataTable(self.db, 'disciplines') def test_columnDefs(self): expected = [ {'type': 'int(10) unsigned', 'name': 'discipline_id'}, {'type': 'varchar(30)', 'name': 'code'}, {'type': 'varchar(45)', 'name': 'name'}, {'type': 'varchar(255)', 'name': 'description'}, {'type': 'int(10) unsigned', 'name': 'active'}, {'type': 'timestamp', 'name': 'last_updated'}, ] sort = lambda r: sorted(r, key=lambda x: x['name']) results = sort(self.datatable.columnDefs()) actual = sort(results) expected = sort(expected) for old, new in zip(expected, actual): self.assertDictEqual(old, new)
def bucket(self, *fields): '''Returns a dict of bucket -> DataTable of rows matching that bucket''' buckets = defaultdict(lambda: []) for data in self: key = tuple(data[field] for field in fields) buckets[key].append(data) return AttributeDict( (key, DataTable(bucket)) for key, bucket in buckets.items())
def fromDataUrl(url): u = urlopen(url) s = BeautifulSoup(u.read()) u.close() headers = [td.string for td in s.find('thead').find_children('td')] data = [[td.string for td in tr.find_children('td')] for tr in s.find('tbody').find_children('tr')] return DataTable(dict(zip(headers, row)) for row in data)
def test_4_find_correct_value(self): import pandas as pd from datatable import DataTable my_data = DataTable("data.csv") my_result = [] for match in my_data.find_n(3, where={ "Year": "2017", "Variable_code": "H01" }): my_result.append(match[8]) real_data = pd.read_csv("data.csv") real_find = real_data[(real_data["Year"] == 2017) & (real_data["Variable_code"] == "H01")] real_result = list(real_find.iloc[:3]["Value"]) self.assertEqual(my_result, real_result)
def delete_item(self): target = self.ids.selectid.text conn = sqlite3.connect("expenditure.db") cur = conn.cursor() cur.execute('DELETE from expenses where id = ?', (target, )) conn.commit() entry = self.ids.catsearch.get_items() entrytable = DataTable(table=entry) self.ids.catsearch.clear_widgets() self.ids.catsearch.add_widget(entrytable)
def custom_datatable(): """ data_query_pandas 와 동일한 내용을, pandas같은 라이브러리를 사용하지 않고 직접 class 로 구현하려고 합니다. Task 2. DataTable 객체는 find_n 이라는 함수를 완성해 같은 결과가 나오도록 구현해 주세요. 구현이 완성된 DataTable 은 tests 에 명시된 단위테스트를 통과해야 합니다. (평가요소 : 가독성 + 메소드 성능 + 메모리 효율 ) :return: """ from datatable import DataTable data_table = DataTable("data.csv") res = [] for match in data_table.find_n(3, where={ "Year": "2017", "Variable_code": "H01", }): res.append(match[8]) return res
def parseFixedWidth(f, headers): ''' Parses a Fixed Width file using the given headers headers are a list of the tuples: (name, start, end), where name is the hame of the column, start is the (1-based) index of the beginning of the column and end is the index of the end of the column ''' headers = [(header[0], int(header[1]) - 1, int(header[2])) for header in headers] def parse(): for line in f: d = AttributeDict() for header in headers: d[header[0]] = line[header[1]:header[2]] yield d return DataTable(parse())
def search_cat(self): target = self.ids.cat.text conn = sqlite3.connect("expenditure.db") cur = conn.cursor() _entries = OrderedDict() _entries['idlist'] = {} _entries['amountlist'] = {} _entries['categorylist'] = {} _entries['messagelist'] = {} _entries['datelist'] = {} idlist = [] amountlist = [] categorylist = [] messagelist = [] datelist = [] rows = None if target == "All": cur.execute('SELECT * FROM expenses') rows = cur.fetchall() else: cur.execute('SELECT * FROM expenses WHERE category=?', (target, )) rows = cur.fetchall() for id1, amt, cat, price, dt in rows: idlist.append(id1) amountlist.append(amt) categorylist.append(cat) messagelist.append(price) datelist.append(dt) entrynum = len(amountlist) idx = 0 while idx < entrynum: _entries['idlist'][idx] = idlist[idx] _entries['amountlist'][idx] = amountlist[idx] _entries['categorylist'][idx] = categorylist[idx] _entries['messagelist'][idx] = messagelist[idx] _entries['datelist'][idx] = datelist[idx] idx += 1 entrytable = DataTable(table=_entries) self.ids.catsearch.clear_widgets() self.ids.catsearch.add_widget(entrytable)
class TestDataTable(unittest.TestCase): '''Test DataTable''' def setUp(self): import db from datatable import DataTable self.db = db.getInstance() self.datatable = DataTable(self.db, 'disciplines') def test_columnDefs(self): expected = [ { 'type': 'int(10) unsigned', 'name': 'discipline_id' }, { 'type': 'varchar(30)', 'name': 'code' }, { 'type': 'varchar(45)', 'name': 'name' }, { 'type': 'varchar(255)', 'name': 'description' }, { 'type': 'int(10) unsigned', 'name': 'active' }, { 'type': 'timestamp', 'name': 'last_updated' }, ] sort = lambda r: sorted(r, key=lambda x: x['name']) results = sort(self.datatable.columnDefs()) actual = sort(results) expected = sort(expected) for old, new in zip(expected, actual): self.assertDictEqual(old, new)
def confirmed_add(self, instance): tempday = str(self.ids.expenseday.text) tempmonth = str(self.ids.expensemonth.text) tempyear = str(self.ids.expenseyear.text) date = tempyear + "-" + tempmonth + "-" + tempday conn = sqlite3.connect("expenditure.db") cur = conn.cursor() cur.execute( """ INSERT INTO expenses (amount,category,message,date) VALUES (?,?,?,?)""", (self.amount.text, self.category.text, self.message.text, date)) conn.commit() conn.close() self.amount.text = "" self.category.text = "" self.message.text = "" self.parent.ids.scnd.ids.catsearch.clear_widgets() entry = self.parent.ids.scnd.itemlist.get_items(self) entrytable = DataTable(table=entry) target = self.parent.ids.scnd.ids.catsearch target.add_widget(entrytable) self.success()
def test_1_init(self): from datatable import DataTable DataTable("data.csv") self.assertTrue(True)
def originalFromRows(self): '''return the original rows being diffed from''' return DataTable( fromRow for result in self for fromRow in result.originalFromRows(self.keyFields))
def parseCsv(f, headers=None, sep=',', quot='"'): return DataTable( AttributeDict(line) for line in csv.DictReader( f, fieldnames=headers, delimiter=sep, quotechar=quot))
def setUp(self): import db from datatable import DataTable self.db = db.getInstance() self.datatable = DataTable(self.db, 'disciplines')
def __call__(self, doc): chartstyle = style.Style(name="chartstyle", family="chart") chartstyle.addElement( style.GraphicProperties(stroke="none", fillcolor="#ffffff")) doc.automaticstyles.addElement(chartstyle) mychart = chart.Chart(width=self.width, height=self.height, stylename=chartstyle, attributes={'class': self.charttype}) doc.chart.addElement(mychart) # Title if self.title: titlestyle = style.Style(name="titlestyle", family="chart") titlestyle.addElement( style.GraphicProperties(stroke="none", fill="none")) titlestyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'", fontfamilygeneric="swiss", fontpitch="variable", fontsize="13pt")) doc.automaticstyles.addElement(titlestyle) mytitle = chart.Title(x="185pt", y="27pt", stylename=titlestyle) mytitle.addElement(text.P(text=self.title)) mychart.addElement(mytitle) # Subtitle if self.subtitle: subtitlestyle = style.Style(name="subtitlestyle", family="chart") subtitlestyle.addElement( style.GraphicProperties(stroke="none", fill="none")) subtitlestyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'", fontfamilygeneric="swiss", fontpitch="variable", fontsize="10pt")) doc.automaticstyles.addElement(subtitlestyle) subtitle = chart.Subtitle(x="50pt", y="50pt", stylename=subtitlestyle) subtitle.addElement(text.P(text=self.subtitle)) mychart.addElement(subtitle) # Legend legendstyle = style.Style(name="legendstyle", family="chart") legendstyle.addElement(style.GraphicProperties(fill="none")) legendstyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'", fontfamilygeneric="swiss", fontpitch="variable", fontsize="8pt")) doc.automaticstyles.addElement(legendstyle) mylegend = chart.Legend(legendposition="end", legendalign="center", stylename=legendstyle) mychart.addElement(mylegend) # Plot area plotstyle = style.Style(name="plotstyle", family="chart") if self.subtype == "stacked": percentage = "false" stacked = "true" elif self.subtype == "percentage": percentage = "true" stacked = "false" else: percentage = "false" stacked = "false" plotstyle.addElement( style.ChartProperties(seriessource="columns", percentage=percentage, stacked=stacked, threedimensional=self.threedimensional)) doc.automaticstyles.addElement(plotstyle) plotarea = chart.PlotArea(datasourcehaslabels=self.datasourcehaslabels, stylename=plotstyle) mychart.addElement(plotarea) # Style for the X,Y axes axisstyle = style.Style(name="axisstyle", family="chart") axisstyle.addElement(style.ChartProperties(displaylabel="true")) axisstyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'", fontfamilygeneric="swiss", fontpitch="variable", fontsize="8pt")) doc.automaticstyles.addElement(axisstyle) # Title for the X axis xaxis = chart.Axis(dimension="x", name="primary-x", stylename=axisstyle) plotarea.addElement(xaxis) xt = chart.Title() xaxis.addElement(xt) xt.addElement(text.P(text=self.x_axis)) # Title for the Y axis yaxis = chart.Axis(dimension="y", name="primary-y", stylename=axisstyle) plotarea.addElement(yaxis) yt = chart.Title() yaxis.addElement(yt) yt.addElement(text.P(text=self.y_axis)) # chart colors piestyle = style.Style(name="series", family="chart") piestyle.addElement(style.GraphicProperties(fillcolor="#000000")) doc.automaticstyles.addElement(piestyle) piestyle = style.Style(name="piestyle.critical", family="chart") piestyle.addElement(style.ChartProperties(solidtype="cuboid")) piestyle.addElement(style.GraphicProperties(fillcolor=riskCritical)) doc.automaticstyles.addElement(piestyle) piestyle = style.Style(name="piestyle.high", family="chart") piestyle.addElement(style.ChartProperties(solidtype="cuboid")) piestyle.addElement(style.GraphicProperties(fillcolor=riskHigh)) doc.automaticstyles.addElement(piestyle) piestyle = style.Style(name="piestyle.medium", family="chart") piestyle.addElement(style.ChartProperties(solidtype="cuboid")) piestyle.addElement(style.GraphicProperties(fillcolor=riskMedium)) doc.automaticstyles.addElement(piestyle) piestyle = style.Style(name="piestyle.low", family="chart") piestyle.addElement(style.ChartProperties(solidtype="cuboid")) piestyle.addElement(style.GraphicProperties(fillcolor=riskLow)) doc.automaticstyles.addElement(piestyle) piestyle = style.Style(name="piestyle.none", family="chart") piestyle.addElement(style.ChartProperties(solidtype="cuboid")) piestyle.addElement(style.GraphicProperties(fillcolor=riskNone)) doc.automaticstyles.addElement(piestyle) # Set up the data series. OOo doesn't show correctly without them. s = chart.Series(valuescellrangeaddress="local-table.$B$2:.$B$6", labelcelladdress="local-table.$B$1", attributes={'class': self.charttype}, stylename="series") s.addElement(chart.DataPoint(stylename="piestyle.critical")) s.addElement(chart.DataPoint(stylename="piestyle.high")) s.addElement(chart.DataPoint(stylename="piestyle.medium")) s.addElement(chart.DataPoint(stylename="piestyle.low")) s.addElement(chart.DataPoint(stylename="piestyle.none")) plotarea.addElement(s) # The data are placed in a table inside the chart object - but could also be a # table in the main document datatable = DataTable(self.values) datatable.datasourcehaslabels = self.datasourcehaslabels mychart.addElement(datatable())
def getSummaryStats(data): summary_vals = defaultdict(list) cnt = len(data["name"]) for i in range(cnt): key = "|".join([data[c][i] for c in category_cols]) summary_vals[key].append(data["price"][i]) for key, vals in summary_vals.iteritems(): a = numpy.array(vals) # print key,':',numpy.mean(a),numpy.std(a),len(vals) return summary_vals print "Loading data" dt = DataTable(FILENAME) dt.split(0.10, True) print "Table size:", dt.printInfo() print "Transforming" dt.apply(getSize, "name", "size", False) dt.apply(getBrand, "name", "brand", False) dt.shuffle() dt_train = dt.copy() dt_train.split(0.75, True) print "Training Table size:", dt_train.printInfo() dt_test = dt.copy()
def __call__(self, doc): chartstyle = style.Style(name="chartstyle", family="chart") chartstyle.addElement( style.GraphicProperties(stroke="none", fillcolor="#ffffff")) doc.automaticstyles.addElement(chartstyle) mychart = chart.Chart( width="576pt", height="504pt", stylename=chartstyle, attributes={'class':self.charttype}) doc.chart.addElement(mychart) # Title if self.title: titlestyle = style.Style(name="titlestyle", family="chart") titlestyle.addElement( style.GraphicProperties(stroke="none", fill="none")) titlestyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'", fontfamilygeneric="swiss", fontpitch="variable", fontsize="13pt")) doc.automaticstyles.addElement(titlestyle) mytitle = chart.Title(x="385pt", y="27pt", stylename=titlestyle) mytitle.addElement( text.P(text=self.title)) mychart.addElement(mytitle) # Subtitle if self.subtitle: subtitlestyle = style.Style(name="subtitlestyle", family="chart") subtitlestyle.addElement( style.GraphicProperties(stroke="none", fill="none")) subtitlestyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'", fontfamilygeneric="swiss", fontpitch="variable", fontsize="10pt")) doc.automaticstyles.addElement(subtitlestyle) subtitle = chart.Subtitle(x="0pt", y="123pt", stylename=subtitlestyle) subtitle.addElement( text.P(text= self.subtitle)) mychart.addElement(subtitle) # Legend legendstyle = style.Style(name="legendstyle", family="chart") legendstyle.addElement( style.GraphicProperties(fill="none")) legendstyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'", fontfamilygeneric="swiss", fontpitch="variable", fontsize="6pt")) doc.automaticstyles.addElement(legendstyle) mylegend = chart.Legend(legendposition="end", legendalign="center", stylename=legendstyle) mychart.addElement(mylegend) # Plot area plotstyle = style.Style(name="plotstyle", family="chart") if self.subtype == "stacked": percentage="false"; stacked="true" elif self.subtype == "percentage": percentage="true"; stacked="false" else: percentage="false"; stacked="false" plotstyle.addElement( style.ChartProperties(seriessource="columns", percentage=percentage, stacked=stacked, threedimensional=self.threedimensional)) doc.automaticstyles.addElement(plotstyle) plotarea = chart.PlotArea(datasourcehaslabels=self.datasourcehaslabels, stylename=plotstyle) mychart.addElement(plotarea) # Style for the X,Y axes axisstyle = style.Style(name="axisstyle", family="chart") axisstyle.addElement( style.ChartProperties(displaylabel="true")) doc.automaticstyles.addElement(axisstyle) # Title for the X axis xaxis = chart.Axis(dimension="x", name="primary-x", stylename=axisstyle) plotarea.addElement(xaxis) xt = chart.Title() xaxis.addElement(xt) xt.addElement(text.P(text=self.x_axis)) # Title for the Y axis yaxis = chart.Axis(dimension="y", name="primary-y", stylename=axisstyle) plotarea.addElement(yaxis) yt = chart.Title() yaxis.addElement(yt) yt.addElement(text.P(text=self.y_axis)) # Data area datatable = DataTable( self.values ) datatable.datasourcehaslabels = self.datasourcehaslabels mychart.addElement(datatable())
def _fromHtmlTable(table): headers = [str(th.string) for th in table.tr.findChildren('th')] rows = [[str(td.string) for td in tr.findChildren('td')] for tr in table.findChildren('tr') if tr.td] return DataTable([headers] + rows)
def __init__(self, **kwargs): super().__init__(**kwargs) entry = self.get_items() entrytable = DataTable(table=entry) self.add_widget(entrytable)
def toTable(self): return DataTable(self)
def fromXML(s): '''Expects s to be an xml string For each child of the root node named "row", adds a datatable row and pulls the attributes into that row ''' return DataTable(row.attrs for row in BeautifulSoup(s).find_child('row'))
def test_2_wrong_source(self): from datatable import DataTable with self.assertRaises(FileNotFoundError): DataTable("wrong_source.csv")
def sorted(self, *fields): def key(row): return tuple(sortKey(row.get(field, None)) for field in fields) return DataTable(sorted(self, key=key))
def originalToRows(self): '''return the original rows being diffed to''' return DataTable(toRow for result in self for toRow in result.originalToRows(self.keyFields))
def __call__(self, doc): chartstyle = style.Style(name="chartstyle", family="chart") chartstyle.addElement( style.GraphicProperties(stroke="none", fillcolor="#ffffff")) doc.automaticstyles.addElement(chartstyle) mychart = chart.Chart(width=self.width, height=self.height,stylename=chartstyle, attributes={'class':self.charttype}) doc.chart.addElement(mychart) # Title if self.title: titlestyle = style.Style(name="titlestyle", family="chart") titlestyle.addElement( style.GraphicProperties(stroke="none", fill="none")) titlestyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'",fontfamilygeneric="swiss", fontpitch="variable", fontsize="13pt")) doc.automaticstyles.addElement(titlestyle) mytitle = chart.Title(x="185pt", y="27pt", stylename=titlestyle) mytitle.addElement( text.P(text=self.title)) mychart.addElement(mytitle) # Subtitle if self.subtitle: subtitlestyle = style.Style(name="subtitlestyle", family="chart") subtitlestyle.addElement( style.GraphicProperties(stroke="none", fill="none")) subtitlestyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'",fontfamilygeneric="swiss", fontpitch="variable", fontsize="10pt")) doc.automaticstyles.addElement(subtitlestyle) subtitle = chart.Subtitle(x="50pt", y="50pt", stylename=subtitlestyle) subtitle.addElement( text.P(text= self.subtitle)) mychart.addElement(subtitle) # Legend legendstyle = style.Style(name="legendstyle", family="chart") legendstyle.addElement( style.GraphicProperties(fill="none")) legendstyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'",fontfamilygeneric="swiss", fontpitch="variable", fontsize="8pt")) doc.automaticstyles.addElement(legendstyle) mylegend = chart.Legend(legendposition="end", legendalign="center", stylename=legendstyle) mychart.addElement(mylegend) # Plot area plotstyle = style.Style(name="plotstyle", family="chart") if self.subtype == "stacked": percentage="false"; stacked="true" elif self.subtype == "percentage": percentage="true"; stacked="false" else: percentage="false"; stacked="false" plotstyle.addElement( style.ChartProperties(seriessource="columns",percentage=percentage, stacked=stacked,threedimensional=self.threedimensional)) doc.automaticstyles.addElement(plotstyle) plotarea = chart.PlotArea(datasourcehaslabels=self.datasourcehaslabels, stylename=plotstyle) mychart.addElement(plotarea) # Style for the X,Y axes axisstyle = style.Style(name="axisstyle", family="chart") axisstyle.addElement( style.ChartProperties(displaylabel="true")) axisstyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'", fontfamilygeneric="swiss", fontpitch="variable", fontsize="8pt")) doc.automaticstyles.addElement(axisstyle) # Title for the X axis xaxis = chart.Axis(dimension="x", name="primary-x", stylename=axisstyle) plotarea.addElement(xaxis) xt = chart.Title() xaxis.addElement(xt) xt.addElement(text.P(text=self.x_axis)) # Title for the Y axis yaxis = chart.Axis(dimension="y", name="primary-y", stylename=axisstyle) plotarea.addElement(yaxis) yt = chart.Title() yaxis.addElement(yt) yt.addElement(text.P(text=self.y_axis)) # chart colors piestyle = style.Style(name="series", family="chart") piestyle.addElement( style.GraphicProperties(fillcolor="#000000")) doc.automaticstyles.addElement(piestyle) piestyle = style.Style(name="piestyle.critical", family="chart") piestyle.addElement( style.ChartProperties(solidtype="cuboid")) piestyle.addElement( style.GraphicProperties(fillcolor=riskCritical)) doc.automaticstyles.addElement(piestyle) piestyle = style.Style(name="piestyle.high", family="chart") piestyle.addElement( style.ChartProperties(solidtype="cuboid")) piestyle.addElement( style.GraphicProperties(fillcolor=riskHigh)) doc.automaticstyles.addElement(piestyle) piestyle = style.Style(name="piestyle.medium", family="chart") piestyle.addElement( style.ChartProperties(solidtype="cuboid")) piestyle.addElement( style.GraphicProperties(fillcolor=riskMedium)) doc.automaticstyles.addElement(piestyle) piestyle = style.Style(name="piestyle.low", family="chart") piestyle.addElement( style.ChartProperties(solidtype="cuboid")) piestyle.addElement( style.GraphicProperties(fillcolor=riskLow)) doc.automaticstyles.addElement(piestyle) piestyle = style.Style(name="piestyle.none", family="chart") piestyle.addElement( style.ChartProperties(solidtype="cuboid")) piestyle.addElement( style.GraphicProperties(fillcolor=riskNone)) doc.automaticstyles.addElement(piestyle) # Set up the data series. OOo doesn't show correctly without them. s = chart.Series(valuescellrangeaddress="local-table.$B$2:.$B$6", labelcelladdress="local-table.$B$1",attributes={'class':self.charttype}, stylename="series") s.addElement(chart.DataPoint(stylename="piestyle.critical")) s.addElement(chart.DataPoint(stylename="piestyle.high")) s.addElement(chart.DataPoint(stylename="piestyle.medium")) s.addElement(chart.DataPoint(stylename="piestyle.low")) s.addElement(chart.DataPoint(stylename="piestyle.none")) plotarea.addElement(s) # The data are placed in a table inside the chart object - but could also be a # table in the main document datatable = DataTable(self.values) datatable.datasourcehaslabels = self.datasourcehaslabels mychart.addElement(datatable())
# DangerouslySetInnerHTML(''' # <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css"> # '''), html.Link(href='/static/css/jquery.dataTables.min.css', rel='stylesheet'), # html.Link(href='/static/css/dash.css', rel='stylesheet'), html.H4('Continents'), dcc.Dropdown( id='continents', options=[{'label': i, 'value': i} for i in DF['continent'].unique()], ), html.H4('DataTable'), DataTable( id='example', columns=DF.columns.tolist(), data=DF.values.tolist(), ), ]) @app.server.route('/static/<path:path>') def static_file(path): static_folder = os.path.join(os.getcwd(), 'static') return send_from_directory(static_folder, path) @app.callback( Output('example', 'data'), [Input('continents', 'value')]) def update_table(continent):
def fromCursor(cur, scrub=None, customScrub=None, indexedResults=False, index=None): '''Expects cur to be a pysql 2.0 - style cursor and returns a (list of) DataTable(s) with the results optional parameter scrub is a method which is called for each header (row from cursor.description) to return a replace method which is then called on each value for that header return None to do nothing on that header optional parameters indexedResults and index are used to determine if the results should be collected in an indexed Hierarchy and what index to use in that Hierarchy example - using adodbapi to connect to MS SQL server, the following will normalize smalldatetime fields to date objects and datetime fields to datetime objects: def parseCursor(cursor): def scrub(header): if header[1] == 135 and header[5] == 0: #135 is the sql datetime type, header[5] is the size of the field def toDate(dt): if isinstance(dt, datetime.datetime): return dt.date() return dt return toDate elif header[1] == 135: def toDateTime(dt): if isinstance(dt, datetime.date) and not isinstance(dt, datetime.datetime): return datetime.datetime(dt.year, dt.month, dt.day) return dt return toDateTime return None return fromCursor(cursor, scrub) ''' if not cur.description: return DataTable() def result(): if not cur.description: return None headers = [h[0] for h in cur.description] def zipHeaders(row): d = {} for i, h in enumerate(headers): if h not in d or d[h] is None: d[h] = row[i] elif row[i] and row[i] != d[h]: print( 'WARNING: query returned multiple columns with the same name (%s) with conflicting data. Taking the data from the first returned column' % h) return d theData = [zipHeaders(row) for row in cur.fetchall()] if scrub is not None: for desc in cur.description: replace = scrub(desc) if replace is not None: for row in theData: row[desc[0]] = replace(row[desc[0]]) if customScrub: for row in theData: for header, fromDbValue in customScrub.items(): if row[header] is not None: try: row[header] = fromDbValue(row[header]) except: pass if indexedResults: return Hierarchy.fromTable(theData, index, set(headers).difference(index)) return DataTable(theData) results = [result()] while cur.nextset(): r = result() if r: results.append(r) if len(results) == 1: return results[0] return results