Drop row factories; add ticker search API

kivy_mainline_and_py3.8
Tyler Goodlet 2018-03-02 21:20:51 -05:00
parent fc45e1215d
commit 1e7dc38a50
1 changed files with 34 additions and 29 deletions

View File

@ -50,7 +50,7 @@ _kv = (f'''
<Cell>
font_size: 18
text_size: self.size
# text_size: self.size
size: self.texture_size
color: {colorcode('gray')}
font_color: {colorcode('gray')}
@ -59,7 +59,7 @@ _kv = (f'''
background_normal: ''
valign: 'middle'
halign: 'center'
outline_color: [0.1]*4
# outline_color: [0.1]*4
<HeaderCell>
font_size: 20
@ -87,9 +87,10 @@ _kv = (f'''
# minimum_width: 200
# row_force_default: True
# row_default_height: 75
outline_color: [.7]*4
# outline_color: [.7]*4
<SearchBar>
# part of the `PagerView`
size_hint: 1, 0.03
font_size: 25
background_color: [0.13]*3 + [1]
@ -172,8 +173,9 @@ class HeaderCell(Button):
"""Column header cell label.
"""
def on_press(self, value=None):
# clicking on a col header indicates to rows by this column
# in `update_quotes()`
"""Clicking on a col header indicates to sort rows by this column
in `update_quotes()`.
"""
table = self.row.table
if self.row.is_header:
table.sort_key = self.key
@ -215,7 +217,7 @@ class BidAskLayout(StackLayout):
cell_type = HeaderCell if header else Cell
top_size = cell_type().font_size
small_size = top_size - 5
top_prop = 0.7
top_prop = 0.7 # proportion of size used by top cell
bottom_prop = 1 - top_prop
for (key, size_hint, font_size), value in zip(
[('last', (1, top_prop), top_size),
@ -375,22 +377,18 @@ class TickerTable(GridLayout):
):
self.add_widget(row) # row append
def header_row(headers, **kwargs):
"""Create a single "header" row from a sequence of keys.
def ticker_search(self, patt):
"""Return sequence of matches when pattern ``patt`` is in a
symbol name. Most naive algo possible for the moment.
"""
headers_dict = {key: key for key in headers}
row = Row(headers_dict, headers=headers, is_header_row=True, **kwargs)
return row
for symbol, row in self.symbols2rows.items():
if patt in symbol:
yield symbol, row
def ticker_table(quotes, **kwargs):
"""Create a new ticker table from a list of quote dicts.
def search(self, patt):
"""Search bar api compat.
"""
table = TickerTable(cols=1, **kwargs)
for ticker_record in quotes:
table.append_row(ticker_record)
return table
return dict(self.ticker_search(patt)) or {}
async def update_quotes(
@ -484,21 +482,29 @@ async def _async_main(name, watchlists, brokermod):
# build out UI
Window.set_title(f"watchlist: {name}\t(press ? for help)")
Builder.load_string(_kv)
# anchor = AnchorLayout(anchor_x='right', anchor_y='bottom')
box = BoxLayout(orientation='vertical', padding=5, spacing=5)
# anchor.add_widget(box)
header = header_row(
first_quotes[0].keys(),
# add header row
headers = first_quotes[0].keys()
header = Row(
{key: key for key in headers},
headers=headers,
is_header_row=True,
size_hint=(1, None),
)
box.add_widget(header)
grid = ticker_table(
first_quotes,
# build grid
grid = TickerTable(
cols=1,
size_hint=(1, None),
)
for ticker_record in first_quotes:
grid.append_row(ticker_record)
# associate the col headers row with the ticker table even though
# they're technically wrapped separately in containing BoxLayout
header.table = grid
# mark the initial sorted column header as bold and underlined
sort_cell = header.get_cell(grid.sort_key)
sort_cell.bold = sort_cell.underline = True
@ -506,8 +512,7 @@ async def _async_main(name, watchlists, brokermod):
# set up a pager view for large ticker lists
grid.bind(minimum_height=grid.setter('height'))
pager = PagerView(box, nursery)
pager.add_widget(grid)
pager = PagerView(box, grid, nursery)
box.add_widget(pager)
widgets = {