main.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. from taipy import Gui
  2. from taipy.gui import Html, navigate
  3. import taipy.gui.builder as tgb
  4. import data
  5. import csv
  6. import graphs
  7. import chat
  8. from datetime import datetime
  9. import pandas as pd
  10. import os
  11. import requests
  12. from fossildata import dataset_fossil_fuels_gdp
  13. from pages.carbonfootprint import carbonfootprint_page
  14. from pages.carbonemissions import carbonemissionspage
  15. from pages.oddeven import oddEvencontent
  16. from pages.greenzone import greenZonecontent
  17. from pages.transport import publicTransportcontent
  18. # --------------------------------------------------------- setting up global styling
  19. stylekit = {
  20. "color_primary": "#FFB6C1",
  21. "color_secondary": "#FFC0CB",
  22. }
  23. # ---------------------------------------------------------
  24. OE = "OddEven"
  25. PT = "PublicTransport"
  26. EV = "ElectricVehicle"
  27. DATE = "Date"
  28. BREAKTYPE = "Break"
  29. button_ids = {
  30. "understandSubmit": "OddEven",
  31. "benefitsSubmit": "OddEven",
  32. "challengeSubmit": "OddEven",
  33. "busTrainSubmit": "PublicTransport",
  34. "bikeScooterSubmit": "PublicTransport",
  35. "carPoolSubmit": "PublicTransport",
  36. "evSubmit": "ElectricVehicle",
  37. "readingSubmit": "ElectricVehicle",
  38. "greenOfficeSubmit": "ElectricVehicle"
  39. }
  40. data.refresh_data()
  41. dataframe = pd.DataFrame({DATE:graphs.date_lis,
  42. OE:graphs.OE_breaks,
  43. EV:graphs.EV_breaks,
  44. PT:graphs.PT_breaks})
  45. def add_entry(activity_category):
  46. current_date = datetime.now().strftime("%m/%d/%y")
  47. data_list = [current_date, activity_category]
  48. with open('test.csv', 'a', newline='') as csvfile:
  49. csv_writer = csv.writer(csvfile)
  50. csv_writer.writerow(data_list)
  51. def on_action(state, id):
  52. # Code to update graph
  53. if id == "loginSubmit":
  54. pages = dashboardpage
  55. pass
  56. elif id == "logoutSubmit":
  57. pages = loginContent
  58. elif id in button_ids:
  59. add_entry(button_ids.get(id))
  60. navigate(state, "analytics")
  61. data.refresh_data()
  62. data_dict = {
  63. DATE:graphs.get_date_lis(),
  64. OE:graphs.get_OE(),
  65. EV:graphs.get_EV(),
  66. PT:graphs.get_PT()
  67. }
  68. state.dataframe = pd.DataFrame(data_dict)
  69. # --------------------------------------------------------- Login Page
  70. loginContent = Html("""""")
  71. # STORE USERNAME AND PASSWORD
  72. user = "user123"
  73. password = "password123"
  74. # navigates to home function
  75. def nav_home(state):
  76. global user
  77. global password
  78. navigate(state, "dashboard")
  79. pass
  80. def updateUser(state):
  81. global user
  82. user = state.user
  83. print("Current user is: ", user)
  84. def updatePassword(state):
  85. global password
  86. password = state.password
  87. print("Current password is ", password)
  88. loginContent = """
  89. <img style="margin-bottom: 20px; margin-top: 15px; width: 200px; height: auto; margin-right: 20px;" src='images/FuelFree.png' alt="Description of the image"></img>
  90. <h1>Join FuelFree Today</h1>
  91. <p>Username: </p>
  92. <|{user}|input|on_change=updateUser|>
  93. <p>Password: </p>
  94. <|{password}|input|on_change=updatePassword|password=True|>
  95. <|Login|button|id="loginSubmit"|on_action=nav_home|>
  96. """
  97. # navigates to home function
  98. def nav_login(state):
  99. navigate(state, "login")
  100. pass
  101. # --------------------------------------------------------- Analytics Page
  102. analyticsContent = """
  103. <|toggle|theme|>
  104. <|{dataframe}|chart|properties={data.property_chart}|rebuild|>
  105. """
  106. # --------------------------------------------------------- Chatbot Page
  107. # chatContent = """"""
  108. chatContent = tgb.Page()
  109. userQuestion = "say hello"
  110. properQuestion = False
  111. prompt = "say hello"
  112. answer = ""
  113. wordCount = 40
  114. def updateUserQuestion(state):
  115. global userQuestion
  116. userQuestion = state.userQuestion
  117. print("Current userQuestion is: ", userQuestion)
  118. pass
  119. def submitQuestion(state):
  120. prompt = state.userQuestion
  121. state.answer = chat.genActivity(prompt + " answered in less than " + str(wordCount) +" words")
  122. pass
  123. chatContent = """
  124. <|toggle|theme|>
  125. <h1 style="color:yellow;">Introducing FuelFree's chatbot, CarbonCutter!</h1>
  126. <br/>
  127. <p>Hi, I am CarbonCutter, here to help. I can recommend any ElectricVehicle, energetic, or OddEven activities for you to do!</p>
  128. <|{userQuestion}|input|label="ask here"|on_change=updateUserQuestion|>
  129. <|Ask|button|id="questionSubmit"|on_action=submitQuestion|>
  130. <|{answer}|input|multiline|answer|active={prompt!="" and answer!=""}|class_name=fullwidth|>
  131. """
  132. country = "Spain"
  133. region = "Europe"
  134. lov_region = list(dataset_fossil_fuels_gdp.Entity.unique())
  135. def load_dataset(_country):
  136. """Load dataset for a specific country.
  137. Args:
  138. _country (str): The name of the country.
  139. Returns:
  140. pandas.DataFrame: A DataFrame containing the fossil fuels GDP data for the specified country.
  141. """
  142. dataset_fossil_fuels_gdp_cp = dataset_fossil_fuels_gdp.reset_index()
  143. dataset_fossil_fuels_gdp_cp = dataset_fossil_fuels_gdp_cp[
  144. dataset_fossil_fuels_gdp["Entity"] == _country
  145. ]
  146. return dataset_fossil_fuels_gdp_cp
  147. dataset_fossil_fuels_gdp_cp = load_dataset(country)
  148. def on_change_country(state):
  149. """Update the dataset based on the selected country.
  150. Args:
  151. state (object): The "state" of the variables ran by the program (value changes through selectors)
  152. Returns:
  153. None
  154. """
  155. print("country is:", state.country)
  156. _country = state.country
  157. dataset_fossil_fuels_gdp_cp = load_dataset(_country)
  158. state.dataset_fossil_fuels_gdp_cp = dataset_fossil_fuels_gdp_cp
  159. layout = {"yaxis": {"range": [0, 100000]}, "xaxis": {"range": [1965, 2021]}}
  160. dashboardpage = """
  161. <|toggle|theme|>
  162. # **Fossil Fuel consumption**{: style="color: #7ED957"} by per capita by country*
  163. Data comes from <a href="https://ourworldindata.org/grapher/per-capita-fossil-energy-vs-gdp" target="_blank">Our World in Data</a>
  164. <|{country}|selector|lov={lov_region}|on_change=on_change_country|dropdown|label=Country/Region|>
  165. <|{dataset_fossil_fuels_gdp_cp}|chart|type=plot|x=Year|y=Fossil fuels per capita (kWh)|height=200%|layout={layout}|>
  166. ## **Fossil fuel per capita for**{: style="color: #7ED957"}* <|{country}|>:
  167. <|{dataset_fossil_fuels_gdp_cp}|table|height=400px|width=95%|>
  168. """
  169. # --------------------------------------------------------- Routing between pages
  170. pages = {
  171. "/": "<|menu|lov={page_names}|on_action=menu_action|>",
  172. "login": loginContent,
  173. "dashboard": dashboardpage,
  174. 'carbonemissionspage': carbonemissionspage,
  175. 'carbonfootprint_page': carbonfootprint_page,
  176. "OddEven": oddEvencontent,
  177. "PublicTransport": publicTransportcontent,
  178. "ElectricVehicle": greenZonecontent,
  179. "analytics": analyticsContent,
  180. "chat": chatContent,
  181. }
  182. page_names = [page for page in pages.keys() if page != "/"]
  183. def menu_action(state, action, payload):
  184. page = payload["args"][0]
  185. navigate(state, page)
  186. # --------------------------------------------------------- Display the GUI
  187. if __name__ == "__main__":
  188. # Create the Gui instance with the provided pages and stylekit
  189. gui = Gui(pages=pages)
  190. # Run the GUI with specified parameters
  191. gui.run(
  192. run_browser=False, # Ensure the browser is not opened automatically
  193. use_reloader=True, # Set to True if you are in development
  194. title="FuelFree", # Set the title of the GUI window
  195. )