title: Based on user input in flask form sending Pandas Grouped data to flask template
body: <p>Here is my challenge:
  1.I have the below form in flask using jinja:
  <a href="https://i.stack.imgur.com/iNRul.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>2.Based on the user input of start date,end date and site,i need the following output:
  <a href="https://i.stack.imgur.com/Lg2Xl.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>3.I have read the excel and parse using pandas:</p>
  
  <p>Input Excel Data:
  <a href="https://docs.google.com/spreadsheets/d/1k4QzpbJQTi1dHBA5oMfyuDDS1vHEJjx3GWpw7bfLS0k/edit?usp=sharing" rel="nofollow noreferrer">inputdata.xlsx</a></p>
  
  <p>4.In flask I have created the following:</p>
  
  <pre><code>    ```
      class hofails(object):
          def __init__(self,startdate,enddate,site):
              self.start_date = startdate
              self.end_date = enddate
              self.site = site
              self.results = []
              self.data = self.loadData()
              self.df = self.loadData()
              self.data['Site'] = copy.deepcopy(self.data['CELL'].str[1:9])
              self.data.fillna(0, inplace=True)
  
          def loadData(self):
              import pandas as pd
              excel_file = 'LTE_HO_T.xlsx'
              df = pd.read_excel(excel_file)
              df = df[['PERIOD_START_TIME','SITE', 'CELL', 'NEIGHBOR_CELL', 'Inter eNB HO Execution 
                   Attempts',\
                  'Inter eNB HO Execution Failure Rate', 'Inter eNB HO Execution Successes',\
                  'Early Handover Type1', 'Early Handover Type2', 'Late Handovers']]
  
              return df        
  
          def get_data_by_site(self, site_id):
              return self.df[self.df['SITE'] == site_id]
  
          def get_data_cell(self, cell_data, start_date, end_date):
              cell_data = cell_data[(cell_data['PERIOD_START_TIME'] &gt;= start_date) &amp; 
              (cell_data['PERIOD_START_TIME'] &lt;= end_date)].dropna(axis=1)
  
              cell_data = cell_data[['PERIOD_START_TIME', 'SITE', 'CELL', 'NEIGHBOR_CELL', 'Inter eNB 
                          HO Execution Attempts', \
                         'Inter eNB HO Execution Failure Rate', 'Inter eNB HO Execution Successes', \
                         'Early Handover Type1', 'Early Handover Type2', 'Late Handovers']]
  
              cell_data = cell_data.groupby(['CELL', 'NEIGHBOR_CELL'], as_index=False)['Inter eNB HO 
                          Execution Attempts', \
                                                                 'Inter eNB HO Execution Failure Rate', 
                         'Inter eNB HO Execution Successes', \
                                                                 'Early Handover Type1', 'Early 
                        Handover Type2', 'Late Handovers'].mean()
  
               cell_data = cell_data[cell_data['Inter eNB HO Execution Failure Rate'] &gt; 0.02]
  
               cell_data['Avg. Inter eNB HO Execution Fails'] = cell_data['Inter eNB HO Execution 
               Attempts'] - cell_data['Inter eNB HO Execution Successes']
  
               cell_data = cell_data[['CELL', 'NEIGHBOR_CELL', 'Inter eNB HO Execution Attempts', 
                          'Inter eNB HO Execution Successes',
                          'Avg. Inter eNB HO Execution Fails', 'Inter eNB HO Execution Failure Rate', 
                          'Early Handover Type1','Early Handover Type2', 'Late Handovers']]        
               return cell_data
      ```
  </code></pre>
  
  <p>5.Then in the 'main.py' i have used the following codes:</p>
  
  <pre><code>    ```
      app = Flask(__name__)
      app.config['SECRET_KEY']='devkey'
      class HO_FAILS(FlaskForm):  # Define the form fields and validation parameters
          StartDate = DateField('DatePicker', format="%Y-%m-%d")
          EndDate = DateField('DatePicker', format="%Y-%m-%d")
          site = StringField(validators=[DataRequired()])
  
      @app.route("/", methods=['GET', 'POST'])
      def lte_ho_fails():
          form = HO_FAILS()
          final_result = {}
          from hofails import hofails
          if form.validate_on_submit():
              StartDate = form.StartDate.data
              EndDate = form.EndDate.data
              site = form.site.data
  
          StartDate = StartDate.strftime('%m/%d/%Y')
          EndDate = EndDate.strftime('%m/%d/%Y')
          hof = hofails(StartDate,EndDate,site)
          site_data = hof.get_data_by_site(site)   
          for cell in site_data["CELL"]:
              cell_data = site_data[site_data["CELL"] == cell]
              cell_data_avg = hof.get_data_cell(cell_data, StartDate, EndDate)
              if cell_data_avg.isna().all(): continue
              data_list = []
              davg = dict(cell_data_avg)
              data_list.append(davg)
              final_result[cell] = data_list
  
          return render_template('lte_ho.html', form=form,
                                              title='LTE HO Fail Analysis',
                                              result=final_result,)
      if __name__ == "__main__":
          app.run(debug=True)
  </code></pre>
  
  <p>6.I have created the template as follows:</p>
  
  <pre><code>    ```
      &lt;form method="POST" action="" enctype=multipart/form-data class="form-upload" &gt;
          &lt;span id="upload-form" class="upload-form"&gt;&lt;/span&gt;
            {{ form.hidden_tag() }}
            {{ form.csrf_token }}
  
          &lt;div class="row card"&gt;
              &lt;div class="logo"&gt;
                  &lt;h3 class="text-muted" text-align="center"&gt;LTE HO Fail Analysis&lt;/h3&gt;
              &lt;/div&gt;
              &lt;BR&gt;&lt;/BR&gt;
          &lt;div&gt;
              &lt;table style="width:100%"&gt;
                &lt;tr&gt;
                  &lt;th align="left"&gt;Start Date&lt;/th&gt;
                  &lt;th align="left"&gt;End Date&lt;/th&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td&gt;{{ form.StartDate(class='dtpick', value=StartDate)}}&lt;/td&gt;
                  &lt;td&gt;{{ form.EndDate(class='dtpick', value=EndDate)}}&lt;/td&gt;
  
                &lt;/tr&gt;
                &lt;/table&gt;
              &lt;/div&gt;
              &lt;label class="text-muted"&gt;Site ID&lt;/label&gt;
              &lt;div class="input-group"&gt;
                    {{ form.site(class="form-control form-element") }}
                      &lt;button class="btn btn-primary" id="sub" type="submit"&gt;Calculate&lt;/button&gt;
                  &lt;/div&gt;
  
          &lt;div&gt;
  
  
      {% for key, value in result.items() %}
      &lt;table border&gt;
  
      &lt;tbody&gt;
      &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;Cell Name:{{ key }} &lt;/b&gt;&lt;/td&gt;
  
      &lt;/tr&gt;
  
      {% for i,j in value[0].items() %}
      &lt;tr&gt;
      &lt;td&gt;{{ i }}&lt;/td&gt;
      &lt;td align="center"&gt;{{ j| round(2, 'floor')  }}&lt;/td&gt;
      &lt;/tr&gt;
      {% endfor %}
      &lt;/tbody&gt;
            &lt;/table&gt;
              &lt;br /&gt;
  
       {% endfor %}
  
          &lt;/div&gt;
          &lt;/div&gt;
          &lt;/form&gt;
          &lt;/TD&gt;
          &lt;/TR&gt;
          &lt;/TABLE&gt;
      {% endblock%}
  </code></pre>
  
  <p>7.After running the application, i am able to input selections through the form but when trying to get the result I am getting 'KEY ERROR'.Please let me know if i need to modify get_data_cell(self, cell_data, start_date, end_date) and the for loop being used in the template as well as in def lte_ho_fails() functions</p>