text: #!/usr/bin/env python
  # coding: utf-8
  
  # In[31]:
  
  
  import dash
  import dash_core_components as dcc
  import dash_html_components as html
  from dash.dependencies import Output, Input
  import pandas as pd
  import numpy as np
  import os
  
  
  # In[3]:
  
  
  data = pd.read_csv("avocado.csv")
  data = data.query("type == 'conventional' and region == 'Albany'")
  data["Date"] = pd.to_datetime(data["Date"], format="%Y-%m-%d")
  data.sort_values("Date", inplace=True)
  
  
  # In[34]:
  
  
  external_stylesheets = [
      {
          "href": "https://fonts.googleapis.com/css2?"
                  "family=Lato:wght@400;700&display=swap",
          "rel": "stylesheet",
      },
  ]
  
  
  # In[40]:
  
  
  app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
  server = app.server
  
  app.title = "Avocado Analytics: Understand Your Avocados!"
  
  
  # In[41]:
  
  
  app.layout = html.Div(
      children=[
          html.Div(
              children=[
                  html.P(children="🥑", className="header-emoji"),
                  html.H1(
                      children="Avocado Analytics", className="header-title"
                  ),
                  html.P(
                      children="Analyze the behavior of avocado prices"
                      " and the number of avocados sold in the US"
                      " between 2015 and 2018",
                      className="header-description",
                  ),
              ],
              className="header",
          ),
          html.Div(
              children=[
                  html.Div(
                      children=[
                          html.Div(children="Region", className="menu-title"),
                          dcc.Dropdown(
                              id="region-filter",
                              options=[
                                  {"label": region, "value": region}
                                  for region in np.sort(data.region.unique())
                              ],
                              value="Albany",
                              clearable=False,
                              className="dropdown",
                          ),
                      ]
                  ),
                  html.Div(
                      children=[
                          html.Div(children="Type", className="menu-title"),
                          dcc.Dropdown(
                              id="type-filter",
                              options=[
                                  {"label": avocado_type, "value": avocado_type}
                                  for avocado_type in data.type.unique()
                              ],
                              value="organic",
                              clearable=False,
                              searchable=False,
                              className="dropdown",
                          ),
                      ],
                  ),
                  html.Div(
                      children=[
                          html.Div(
                              children="Date Range",
                              className="menu-title"
                              ),
                          dcc.DatePickerRange(
                              id="date-range",
                              min_date_allowed=data.Date.min().date(),
                              max_date_allowed=data.Date.max().date(),
                              start_date=data.Date.min().date(),
                              end_date=data.Date.max().date(),
                          ),
                      ]
                  ),
              ],
              className="menu",
          ),
          html.Div(
              children=[
                  html.Div(
                      children=dcc.Graph(
                          id="price-chart", config={"displayModeBar": False},
                      ),
                      className="card",
                  ),
                  html.Div(
                      children=dcc.Graph(
                          id="volume-chart", config={"displayModeBar": False},
                      ),
                      className="card",
                  ),
              ],
              className="wrapper",
          ),
      ]
  )
  
  
  # In[42]:
  
  
  @app.callback(
      [Output("price-chart", "figure"), Output("volume-chart", "figure")],
      [
          Input("region-filter", "value"),
          Input("type-filter", "value"),
          Input("date-range", "start_date"),
          Input("date-range", "end_date"),
      ],
  )
  def update_charts(region, avocado_type, start_date, end_date):
      mask = (
          (data.region == region)
          & (data.type == avocado_type)
          & (data.Date >= start_date)
          & (data.Date <= end_date)
      )
      filtered_data = data.loc[mask, :]
      price_chart_figure = {
          "data": [
              {
                  "x": filtered_data["Date"],
                  "y": filtered_data["AveragePrice"],
                  "type": "lines",
                  "hovertemplate": "$%{y:.2f}<extra></extra>",
              },
          ],
          "layout": {
              "title": {
                  "text": "Average Price of Avocados",
                  "x": 0.05,
                  "xanchor": "left",
              },
              "xaxis": {"fixedrange": True},
              "yaxis": {"tickprefix": "$", "fixedrange": True},
              "colorway": ["#17B897"],
          },
      }
  
      volume_chart_figure = {
          "data": [
              {
                  "x": filtered_data["Date"],
                  "y": filtered_data["Total Volume"],
                  "type": "lines",
              },
          ],
          "layout": {
              "title": {"text": "Avocados Sold", "x": 0.05, "xanchor": "left"},
              "xaxis": {"fixedrange": True},
              "yaxis": {"fixedrange": True},
              "colorway": ["#E12D39"],
          },
      }
      return price_chart_figure, volume_chart_figure
  
  
  # In[39]:
  
  #port = int(os.environ.get('PORT', 5000))
  
  if __name__ == "__main__":
  	#app.run_server(host='0.0.0.0', port=port, debug=True, use_reloader=False)
  	app.run_server(debug=False, use_reloader=False)
  
  # In[ ]:
  
  
  
  
  