app.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. # page.title = "ToDo App"
  43. # page.horizontal_alignment = "center"
  44. # page.scroll = "adaptive"
  45. # page.update()
  46. # page.add(app)
  47. content = HomePage()
  48. def menu_changed(e):
  49. ''' menu change evnet '''
  50. content.controls.clear()
  51. if e.control.selected_index == 0:
  52. content.controls.append(HomePage())
  53. elif e.control.selected_index == 1:
  54. content.controls.append(TodoPage())
  55. elif e.control.selected_index == 2:
  56. content.controls.append(ft.Text("商品库!"))
  57. elif e.control.selected_index == 3:
  58. fig = ChartPage().DrawChart()
  59. # fig.show()
  60. content.controls.append(PlotlyChart(fig, expand=True))
  61. elif e.control.selected_index == 4:
  62. content.controls.append(ft.Text("Setting!"))
  63. page.update()
  64. rail = ft.NavigationRail(
  65. selected_index=0,
  66. label_type=ft.NavigationRailLabelType.ALL,
  67. # extended=True,
  68. min_width=100,
  69. min_extended_width=400,
  70. # leading=ft.FloatingActionButton(icon=ft.icons.CREATE, text="Add"),
  71. group_alignment=-0.9,
  72. destinations=[
  73. ft.NavigationRailDestination(
  74. icon=ft.icons.HOME, selected_icon=ft.icons.HOME, label="首页"
  75. ),
  76. ft.NavigationRailDestination(
  77. icon_content=ft.Icon(ft.icons.FAVORITE),
  78. selected_icon_content=ft.Icon(name=ft.icons.FAVORITE, color=ft.colors.PINK),
  79. label="关键词",
  80. ),
  81. ft.NavigationRailDestination(
  82. icon_content=ft.Icon(ft.icons.SHOPIFY),
  83. selected_icon_content=ft.Icon(name=ft.icons.SHOPIFY, color=ft.colors.PINK),
  84. label="商品库",
  85. ),
  86. ft.NavigationRailDestination(
  87. icon_content=ft.Icon(ft.icons.BAR_CHART),
  88. selected_icon_content=ft.Icon(name=ft.icons.BAR_CHART, color=ft.colors.PINK),
  89. label="数据分析",
  90. ),
  91. ft.NavigationRailDestination(
  92. icon=ft.icons.SETTINGS_OUTLINED,
  93. selected_icon_content=ft.Icon(ft.icons.SETTINGS),
  94. label_content=ft.Text("Settings"),
  95. ),
  96. ],
  97. on_change=menu_changed,
  98. )
  99. page.title = 'First App'
  100. page.add(
  101. ft.Row(
  102. [
  103. rail,
  104. ft.VerticalDivider(width=1),
  105. content,
  106. ],
  107. expand=True,
  108. )
  109. )
  110. def run(self):
  111. ''''''
  112. ft.app(target=self.main, view=ft.WEB_BROWSER, host="0.0.0.0", port=8080)