I certainly would find such a feature useful. psuedo-database almost could be a use. Maybe not used in production, but for unit testing and other internal tasks?
On Tuesday, August 29, 2017 at 9:55:32 AM UTC-5, Arjan wrote: > > I'd like to work with tabular data in my Leo outlines, such that the table > structure is visualized inside a node body (aligned column widths) and a > key binding should facilitate navigation/data entry (e.g. tab jumps to next > column). In other words, it should work roughly like OrgMode tables. > > I've started looking at this and it seems the basic transformations aren't > too problematic. But, I'm not very experienced with Python (or other > languages, for that matter), nor with Leo plugins. I've made a first very > rough version which seems to work for the basics. So my questions are: Is > the idea in general likely to work well in Leo? And, how terrible is the > attached approach? ;) > I've made an @button with two functions (see below): text_to_table() turns > lines with |-separators into a list of lists (rows containing cells), and > align_table() aligns the columns in such a table using whitespace, and > returns the whole thing as a string. > > Some specifics I'm wondering about now: > 1) Is it possible to re-assign e.g. the tab key to trigger the alignment, > but only on nodes of a new type (say @tabular)? > 2) Would same be possible for a newly defined "@language tabular", so it > would work on just the tabular section of a node? > 3) The current approach relies on number of characters for aligning, so a > mono-spaced font is required. Is it possible to use a particular font for > just a particular node type (or language)? > 4) Or are there ways of aligning text other than via mono-spaced fonts? > 5) Is it feasible to use two different vnode body formats, where the GUI > format would use the whitespace and visual separator such as |, but the > on-disk format would be transformed to CSV or tab-separated plain text > (stripped of the extra alignment whitespace)? > 6) Note-to-self: there are a couple of libraries which could facilitate > the aligning/format transformations, such as > http://astropy.readthedocs.io/en/latest/io/ascii/index.html, > https://pypi.python.org/pypi/tabulate, > https://github.com/foutaise/texttable/. > > I'm curious to hear your thoughts! > > Best, Arjan > > > @language python > > def text_to_table(texttable): > """ Transforms lines with pipe separators to a table stored as a list > of lists.""" > table = [] > for line in texttable.splitlines(): > # Don't combine stripping of separators and whitespace, in case of > empty columns > line = line.strip() # Strip leading and trailing whitespace > line = line.strip('|') # Strip leading and trailing separators > columns = line.split('|') # Split line into cells > row = [column.strip() for column in columns] # Strip cell > whitespace > table.append(row) > return(table) > > def align_table(table): > """Returns a table, padded for alignment, as a string. > @param table: The table to print. A list of lists. > Each row must have the same number of columns. > After > http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/ > """ > > def get_max_width(table, index): > """Get the maximum width of the given column index.""" > return max([len(row[index]) for row in table]) > > col_paddings = [] > for i in range(len(table[0])): # number of columns in the first row > col_paddings.append(get_max_width(table, i)) > > alignedtable = '' > for row in table: > alignedrow = '|' > for i in range(0, len(row)): > tablecell = row[i].ljust(col_paddings[i] + 1) + '|' > alignedrow = alignedrow + tablecell > alignedrow = alignedrow + '\n' > alignedtable = alignedtable + alignedrow > return(alignedtable) > > texttable = p.b > b = c.undoer.beforeChangeNodeContents(p) > table = text_to_table(texttable) > p.b = align_table(table) > c.undoer.afterChangeNodeContents(p,'tabulate',b) > > > > -- You received this message because you are subscribed to the Google Groups "leo-editor" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/leo-editor. For more options, visit https://groups.google.com/d/optout.
