from remi.server import App, start, Server
import remi.gui as gui
import time
from position_operator.position_operator import PositionJsonOperator as TQZJsonOperator
class FirstApp(App):
def __init__(self, *args):
super(FirstApp, self).__init__(*args)
def idle(self):
# TODO 测试增删数据, 依旧是假数据...
if self._is_refresh_time(now_time_second=time.localtime().tm_sec, interval_time=30) is True:
account_dic = TQZJsonOperator.tqz_load_jsonfile("account.json")
for key, value in account_dic.items():
if key == self.account_list[0] and len(value) < 21:
account_dic[key].append(['1', 'Pan', 'Peter', '888888', '上海']) # 新加 ['1', 'Pan', 'Peter', '888888', '上海'] 数据到最后
TQZJsonOperator.tqz_write_jsonfile(content=account_dic, filename="account.json")
elif key == self.account_list[1] and len(value) < 26:
account_dic[key].append(['1', 'Pan', 'Peter', '888888', '上海']) # 新加 ['1', 'Pan', 'Peter', '888888', '上海'] 数据到最后
TQZJsonOperator.tqz_write_jsonfile(content=account_dic, filename="account.json")
if key == self.main_table_key:
self.main_table_data = account_dic[key]
# 刷新一次 数据源、table控件、time控件
if self._is_refresh_time(now_time_second=time.localtime().tm_sec, interval_time=10) is True:
print(self._time_now())
self._tableData_refresh(refresh_content=self.main_table_data)
def main(self):
self.layout_width = '90%'
self.first_load = True
self.account_list = ["item_one", "item_two"]
self.main_table_data = self._first_load_table_data()
self.main_table_key = self.account_list[0]
self.window = gui.VBox(width='100%') # 全屏
# drop_down控件: 选择账户
self.drop_down = self._get_dropDown(*self.account_list)
# time_label控件: 当前时间
self.time_label = self._get_time_label()
# table控件: 账户对应的持仓数据
self.table = self._get_table_list()
return self._add_subviews(self.drop_down, self.time_label, self.table, window=self.window)
# private part
def _get_time_label(self):
label_test = '更新时间: ' + self._time_now()
return gui.Label(label_test, width=self.layout_width, height='20%') # 控件: label
@staticmethod
def _time_now():
return time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())
@staticmethod
def _dropDown_add_accounts(*account_list, drop_down):
[drop_down.append(gui.DropDownItem(text=element), element) for element in account_list]
def _get_dropDown(self, *account_list):
drop_down = gui.DropDown(width=self.layout_width, height='10%') # 控件: drop_down
self._dropDown_add_accounts(*account_list, drop_down=drop_down)
# 事件监听方法
drop_down.onchange.do(
callback=self._drop_down_change
) # onchange.do 和 set_on_change_listener 只会执行最后绑定的那个方法
# self.drop_down.set_on_change_listener(self._dropDown_on_change_listener)
drop_down.style.update({'font-size': 'small'})
return drop_down
def _drop_down_change(self, widget, *args):
# TODO: load 一些假数据...
content_dic = TQZJsonOperator.tqz_load_jsonfile(filename="account.json")
self.main_table_key = widget.get_key()
self.main_table_data = content_dic[widget.get_value()]
self._tableData_refresh(refresh_content=self.main_table_data)
@staticmethod
def _dropDown_on_change_listener(callback, *args):
print("_dropDown_on_change_listener")
print(callback)
print(args)
def _tableData_refresh(self, refresh_content):
self.window.remove_child(self.table)
self.window.remove_child(self.time_label)
self.time_label = self._get_time_label()
self.table = gui.Table.new_from_list(content=refresh_content, width=self.layout_width,
fill_title=True)
self._add_subviews(self.time_label, self.table, window=self.window)
def _get_table_list(self):
content_dic = TQZJsonOperator.tqz_load_jsonfile(filename="account.json")
return gui.Table.new_from_list(content=content_dic[self.account_list[0]], width=self.layout_width,
fill_title=True) # fill_title: True代表第一行是蓝色, False代表表格内容全部同色
def _first_load_table_data(self):
# table控件: 账户对应的持仓数据, 暂时用一些假数据
table_list = [('ID', 'Lastname', 'Firstname', '邮编', '城市'),
('1', 'Pan', 'Peter', '888888', '上海'),
('2', 'Pan', 'Peter', '888888', '上海'),
('3', 'Pan', 'Peter', '888888', '上海'),
('4', 'Pan', 'Peter', '888888', '上海'),
('5', 'Pan', 'Peter', '888888', '上海'),
('6', 'Pan', 'Peter', '888888', '上海'),
('7', 'Pan', 'Peter', '888888', '上海'),
('8', 'Pan', 'Peter', '888888', '上海'),
('9', 'Pan', 'Peter', '888888', '上海'),
('10', 'Sepp', 'Schmuck', '123456', '北京')]
table_list2 = [('ID', 'Lastname', 'Firstname', '邮编', '城市'),
('1', 'Pan', 'Peter', '888888', '上海'),
('2', 'Pan', 'Peter', '888888', '上海'),
('3', 'Pan', 'Peter', '888888', '上海'),
('4', 'Pan', 'Peter', '888888', '上海'),
('5', 'Pan', 'Peter', '888888', '上海'),
('6', 'Pan', 'Peter', '888888', '上海'),
('7', 'Pan', 'Peter', '888888', '上海'),
('8', 'Pan', 'Peter', '888888', '上海'),
('9', 'Pan', 'Peter', '888888', '上海'),
('10', 'Pan', 'Peter', '888888', '上海'),
('11', 'Pan', 'Peter', '888888', '上海'),
('12', 'Pan', 'Peter', '888888', '上海'),
('13', 'Pan', 'Peter', '888888', '上海'),
('14', 'Pan', 'Peter', '888888', '上海'),
('15', 'Sepp', 'Schmuck', '123456', '北京')]
# TODO App初次加载, 将测试用的假数据 写入 json 文件...
content_dic = {}
if self.first_load is True:
content_dic[self.account_list[0]] = table_list
content_dic[self.account_list[1]] = table_list2
TQZJsonOperator.tqz_write_jsonfile(content=content_dic, filename="account.json")
self.first_load = False
else:
content_dic = TQZJsonOperator.tqz_load_jsonfile(filename="account.json")
return content_dic[self.account_list[0]]
@staticmethod
def _add_subviews(*subviews, window):
[window.append(subview) for subview in subviews]
return window
def _is_refresh_time(self, now_time_second, interval_time):
"""
判断当前时间, 是否应该刷新数据(table_data、table控件、time_label_控件)
:param now_time_second: 当前时间的 秒数
:param interval_time: 刷新一次所用时间
:return: 当前秒数是否应该刷新
"""
if now_time_second % interval_time is 0:
should_refresh = True
else:
should_refresh = False
return should_refresh
if __name__ == '__main__':
# remi库文档资料: https://remi.readthedocs.io/en/latest/remi.html#remi.gui.Widget
Server(gui_class=FirstApp, update_interval=1, start_browser=True) # 参数 update_interval: 程序每1s调用一次 idel() 函数;