app.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2023/12/29 19:12:38
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc :
  8. '''
  9. import flet as ft
  10. from flet import (
  11. Checkbox,
  12. Column,
  13. FloatingActionButton,
  14. IconButton,
  15. OutlinedButton,
  16. Page,
  17. Row,
  18. Tab,
  19. Tabs,
  20. Text,
  21. TextField,
  22. UserControl,
  23. colors,
  24. icons,
  25. )
  26. from flet.plotly_chart import PlotlyChart
  27. import logging
  28. from .pages.home import HomePage
  29. from .pages.plotly_chart import ChartPage
  30. from .pages.todo import TodoPage
  31. class App(UserControl):
  32. ''' build a flet app '''
  33. def __init__(self):
  34. ''' init '''
  35. super().__init__()
  36. log_format = "%(asctime)s - %(levelname)s - %(message)s"
  37. logging.basicConfig(filename='app.log', level=logging.DEBUG, format=log_format)
  38. logging.info('程序启动')
  39. @staticmethod
  40. def main(page: ft.Page):
  41. ''' main page '''
  42. content = HomePage()
  43. def menu_changed(e):
  44. ''' menu change evnet '''
  45. content.controls.clear()
  46. if e.control.selected_index == 0:
  47. content.controls.append(HomePage())
  48. elif e.control.selected_index == 1:
  49. content.controls.append(TodoPage())
  50. elif e.control.selected_index == 2:
  51. content.controls.append(ft.Text("商品库!"))
  52. elif e.control.selected_index == 3:
  53. fig = ChartPage().draw_chart()
  54. # fig.show()
  55. content.controls.append(PlotlyChart(fig, expand=True))
  56. elif e.control.selected_index == 4:
  57. content.controls.append(ft.Text("Setting!"))
  58. page.update()
  59. rail_menu = ft.NavigationRail(
  60. selected_index=0,
  61. label_type=ft.NavigationRailLabelType.ALL,
  62. # extended=True,
  63. min_width=100,
  64. min_extended_width=400,
  65. # leading=ft.FloatingActionButton(icon=ft.icons.CREATE, text="Add"),
  66. group_alignment=-0.9,
  67. destinations=[
  68. ft.NavigationRailDestination(
  69. icon=ft.icons.HOME, selected_icon=ft.icons.HOME, label="首页"
  70. ),
  71. ft.NavigationRailDestination(
  72. icon_content=ft.Icon(ft.icons.FAVORITE),
  73. selected_icon_content = ft.Icon(name=ft.icons.FAVORITE, color=ft.colors.PINK),
  74. label="关键词",
  75. ),
  76. ft.NavigationRailDestination(
  77. icon_content=ft.Icon(ft.icons.SHOPIFY),
  78. selected_icon_content = ft.Icon(name=ft.icons.SHOPIFY, color=ft.colors.PINK),
  79. label="商品库",
  80. ),
  81. ft.NavigationRailDestination(
  82. icon_content=ft.Icon(ft.icons.BAR_CHART),
  83. selected_icon_content = ft.Icon(name=ft.icons.BAR_CHART, color=ft.colors.PINK),
  84. label="数据分析",
  85. ),
  86. ft.NavigationRailDestination(
  87. icon=ft.icons.SETTINGS_OUTLINED,
  88. selected_icon_content = ft.Icon(ft.icons.SETTINGS),
  89. label_content = ft.Text("Settings"),
  90. ),
  91. ],
  92. on_change=menu_changed,
  93. )
  94. page.title = 'First App'
  95. # page.horizontal_alignment = "center"
  96. # page.scroll = "adaptive"
  97. # page.update()
  98. page.add(
  99. ft.Row(
  100. [
  101. rail_menu,
  102. ft.VerticalDivider(width=1),
  103. content,
  104. ],
  105. expand=True,
  106. )
  107. )
  108. def run(self):
  109. ''''''
  110. ft.app(target=self.main, view=ft.WEB_BROWSER, host="0.0.0.0", port=8080)