def table2story(self, ar, column_names=None, header_level=None, header_links=None, nosummary=False, stripped=True, show_links=False, **kwargs): """ Render the given table request as reStructuredText to stdout. See :meth:`ar.show <lino.core.request.BaseRequest.show>`. """ if ar.actor.master is not None and not nosummary: if ar.actor.display_mode == 'summary': s = to_rst( ar.actor.get_table_summary(ar.master_instance, ar), stripped=stripped) if stripped: s = s.strip() yield s return fields, headers, widths = ar.get_field_info(column_names) sums = [fld.zero for fld in fields] rows = [] recno = 0 for row in ar.sliced_data_iterator: recno += 1 if show_links: rows.append([ to_rst(x) for x in ar.row2html( recno, fields, row, sums)]) else: rows.append([x for x in ar.row2text(fields, row, sums)]) if header_level is not None: h = rstgen.header(header_level, ar.get_title()) if stripped: h = h.strip() yield h # s = h + "\n" + s # s = tostring(E.h2(ar.get_title())) + s if len(rows) == 0: s = str(ar.no_data_text) if not stripped: s = "\n" + s + "\n" yield s return if not ar.actor.hide_sums: has_sum = False for i in sums: if i: #~ print '20120914 zero?', repr(i) has_sum = True break if has_sum: rows.append([x for x in ar.sums2html(fields, sums)]) t = RstTable(headers, **kwargs) yield t.to_rst(rows)
def table2story(self, ar, column_names=None, header_level=None, nosummary=False, stripped=True, show_links=False, **kwargs): """ Render the given table request as reStructuredText to stdout. See :meth:`ar.show <lino.core.request.BaseRequest.show>`. """ if ar.actor.master is not None and not nosummary: if ar.actor.display_mode == 'summary': s = to_rst(ar.actor.get_table_summary(ar.master_instance, ar), stripped=stripped) if stripped: s = s.strip() return s fields, headers, widths = ar.get_field_info(column_names) sums = [fld.zero for fld in fields] rows = [] recno = 0 for row in ar.sliced_data_iterator: recno += 1 if show_links: rows.append( [to_rst(x) for x in ar.row2html(recno, fields, row, sums)]) else: rows.append([x for x in ar.row2text(fields, row, sums)]) if len(rows) == 0: s = str(ar.no_data_text) if not stripped: s = "\n" + s + "\n" return s if not ar.actor.hide_sums: has_sum = False for i in sums: if i: #~ print '20120914 zero?', repr(i) has_sum = True break if has_sum: rows.append([x for x in ar.sums2html(fields, sums)]) t = RstTable(headers, **kwargs) s = t.to_rst(rows) if header_level is not None: h = rstgen.header(header_level, ar.get_title()) if stripped: h = h.strip() s = h + "\n" + s # s = tostring(E.h2(ar.get_title())) + s return s
def show_story(self, ar, story, stripped=True, **kwargs): """Render the given story as reStructuredText to stdout.""" from lino.core.actors import Actor from lino.core.tables import TableRequest from lino.core.requests import ActionRequest try: for item in forcetext(story): if iselement(item): print(to_rst(item, stripped)) elif isinstance(item, type) and issubclass(item, Actor): ar = item.default_action.request(parent=ar) self.show_table(ar, stripped=stripped, **kwargs) elif isinstance(item, DashboardItem): self.show_story( ar, item.render(ar), stripped, **kwargs) elif isinstance(item, TableRequest): self.show_table(item, stripped=stripped, **kwargs) # print(item.table2rst(*args, **kwargs)) elif isinstance(item, ActionRequest): # example : courses.StatusReport in dashboard assert item.renderer is not None self.show_story(ar, item.actor.get_story(None, ar), **kwargs) elif isiterable(item): self.show_story(ar, item, stripped, **kwargs) # for i in self.show_story(ar, item, *args, **kwargs): # print(i) else: raise Exception("Cannot handle %r" % item) except Warning as e: print(e)
def ar2workbook(ar, column_names=None): from openpyxl import Workbook from openpyxl.styles import Font # local import to avoid the following traceback: # Error in sys.exitfunc: # Traceback (most recent call last): # File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs # func(*targs, **kargs) # File "/openpyxl/writer/write_only.py", line 38, in _openpyxl_shutdown # for path in ALL_TEMP_FILES: # TypeError: 'NoneType' object is not iterable # workbook = Workbook(guess_types=True) # removed `guess_types=True` because it caused trouble in openpyxl # 3.4.0 and because I don't know whether it is needed. workbook = Workbook() sheet = workbook.active sheet.title = sheet_name(ar.get_title()) bold_font = Font( name='Calibri', size=11, bold=True, ) fields, headers, widths = ar.get_field_info(column_names) for c, column in enumerate(fields): sheet.cell(row=1, column=c + 1).value = str(headers[c]) sheet.cell(row=1, column=c + 1).font = bold_font # sheet.col(c).width = min(256 * widths[c] / 7, 65535) # 256 == 1 character width, max width=65535 for c, column in enumerate(fields): for r, row in enumerate(ar.data_iterator, start=1): sf = column.field._lino_atomizer value = sf.full_value_from_object(row, ar) if type(value) == bool: value = value and 1 or 0 elif isinstance(value, (Duration, Choice)): value = str(value) elif iselement(value): value = to_rst(value) # dd.logger.info("20160716 %s", value) elif isinstance(value, Promise): value = str(value) elif isinstance(value, IncompleteDate): if value.is_complete(): value = value.as_date() else: value = str(value) elif isinstance(value, Model): value = str(value) sheet.cell(row=r + 1, column=c + 1).value = value return workbook