这是我的挑战: 1.我在下面的表格中使用jinja在烧瓶中: enter image description here
2。根据用户输入的开始日期,结束日期和站点,我需要以下输出: enter image description here
3。我已经阅读了excel并使用熊猫进行了解析:
输入Excel数据: inputdata.xlsx
4。在烧瓶中,我创建了以下内容:
```
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'] >= start_date) &
(cell_data['PERIOD_START_TIME'] <= 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'] > 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
```
5。然后在“ main.py”中,我使用了以下代码:
```
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)
6。我创建了如下模板:
```
<form method="POST" action="" enctype=multipart/form-data class="form-upload" >
<span id="upload-form" class="upload-form"></span>
{{ form.hidden_tag() }}
{{ form.csrf_token }}
<div class="row card">
<div class="logo">
<h3 class="text-muted" text-align="center">LTE HO Fail Analysis</h3>
</div>
<BR></BR>
<div>
<table style="width:100%">
<tr>
<th align="left">Start Date</th>
<th align="left">End Date</th>
</tr>
<tr>
<td>{{ form.StartDate(class='dtpick', value=StartDate)}}</td>
<td>{{ form.EndDate(class='dtpick', value=EndDate)}}</td>
</tr>
</table>
</div>
<label class="text-muted">Site ID</label>
<div class="input-group">
{{ form.site(class="form-control form-element") }}
<button class="btn btn-primary" id="sub" type="submit">Calculate</button>
</div>
<div>
{% for key, value in result.items() %}
<table border>
<tbody>
<tr>
<td><b>Cell Name:{{ key }} </b></td>
</tr>
{% for i,j in value[0].items() %}
<tr>
<td>{{ i }}</td>
<td align="center">{{ j| round(2, 'floor') }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<br />
{% endfor %}
</div>
</div>
</form>
</TD>
</TR>
</TABLE>
{% endblock%}
7。运行应用程序后,我可以通过表格输入选择,但是当尝试获取结果时,我会收到“ KEY ERROR”。请告知我是否需要修改get_data_cell(self,cell_data,start_date ,end_date)和模板以及def lte_ho_fails()函数中使用的for循环