Tabular data in Leo outlines

2017-08-29 Thread Arjan
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 leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: jupyter files

2017-08-29 Thread Josef


On Saturday, 26 August 2017 19:40:47 UTC+2, Edward K. Ream wrote:
>
> On Sat, Aug 26, 2017 at 8:30 AM, Josef  
> wrote:
>
> I was hoping to be able to read into an @clean node instead of using @auto 
>> or @nosent, as I need to cooperate with others, not using Leo.
>>
>
> ​Why is this a problem?  Just read the file into @auto the first time, 
> then change it to @clean.​
>

Doh! Of course that is the simplest solution and works fine for the latex 
files.
It won't work out of the box for jupyter files, since these contain all 
kind of stuff which should not go into the normal python node bodies,
but still need to be written back if we want to round-trip jupyter files.
If that other stuff is moved out of the way to be able to run jupyter files 
as scripts in Leo, then we will have to undo this moving out of the way 
when writing jupyter files back.
 

​
>
>> If nested @clean nodes ever become viable, great. In the meantime I stick 
>> to having the @clean nodes all on the same level and linking them to the 
>> nodes in the document tree, where they are \input.
>>
>
> ​I don't see any enhancement request for nested @clean, perhaps I once 
> disparaged (rejected?) such a thing.  Right now it seems a reasonable thing 
> to want.  Please do file an enhancement request, giving a real-world LaTeX 
> example as motivation.
>

The enhancement request already exists, and you filed it yourself a while 
ago, although under a different title:
>> I've just created #525: Ignore @clean in descendants of @others nodes 


I will add a latex example soon, but want to write it to be useful in a 
test case, so it covers all relevant aspects. 
This may still take a bit of thinking.

- Josef

-- 
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 leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.