Hello,
I’m getting this error when trying to get the a download link for an excel file.The application works and gives the output and the diagrams.
Do you guys know what exactly gives this error?
Below I will paste my code,if you need any more information I can do it.
Traceback (most recent call last):
File "C:\Users\denis.akvic\Desktop\BDE\flask\apps\app1.py", line 733, in download_sheet
df["Timestamp"] = df["Timestamp"].dt.tz_localize(None)
File "C:\Users\denis.akvic\AppData\Local\Programs\Python\Python39\Lib\site-packages\pandas\core\generic.py", line 5487, in __getattr__
return object.__getattribute__(self, name)
File "C:\Users\denis.akvic\AppData\Local\Programs\Python\Python39\Lib\site-packages\pandas\core\accessor.py", line 181, in __get__
accessor_obj = self._accessor(obj)
File "C:\Users\denis.akvic\AppData\Local\Programs\Python\Python39\Lib\site-packages\pandas\core\indexes\accessors.py", line 506, in __new__
raise AttributeError("Can only use .dt accessor with datetimelike values")
AttributeError: Can only use .dt accessor with datetimelike values
This is the link12 code
@app.callback(Output("link12", "children"),
[Input("excel_data_error_log", "data")],
state=[State("start_date_picker", "date"),
State("start_time_picker", "value"),
State("end_date_picker", "date"),
State("end_time_picker", "value"),
State("database", "value")])
def download_sheet(output, start_date_picker, start_time_picker, end_date_picker, end_time_picker, database):
if not output:
return None
df = pd.read_json(output, orient='split')
df["Timestamp"] = df["Timestamp"].dt.tz_localize(None)
start_time = datetime.datetime.strptime(start_date_picker, "%Y-%m-%d") + datetime.timedelta(hours=int(start_time_picker[0:2]))
end_time = datetime.datetime.strptime(end_date_picker, "%Y-%m-%d") + datetime.timedelta(hours=int(end_time_picker[0:2]))
xlsx_io = io.BytesIO()
writer = pd.ExcelWriter(xlsx_io, engine='xlsxwriter', options={"remove_timezone" : True})
df.to_excel(writer, sheet_name="Error Log")
writer.save()
xlsx_io.seek(0)
# https://en.wikipedia.org/wiki/Data_URI_scheme
media_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
data = base64.b64encode(xlsx_io.read()).decode("utf-8")
href_data_downloadable = f'data:{media_type};base64,{data}'
file_name = "Error_Log-{0}-{1}_{2}.xlsx".format(str(database), start_time, end_time)
return html.A("Error Log Raw Data", download=file_name, href=href_data_downloadable)
I updated my database and this happened.
This is my code for the sql query
def query_db_parts(n_clicks, db_name, language, start_date_picker, start_time_picker, end_date_picker, end_time_picker, part_type, worker_name):
if n_clicks:
start_time = datetime.datetime.strptime(start_date_picker, "%Y-%m-%d") + datetime.timedelta(hours=int(start_time_picker[0:2]))
end_time = datetime.datetime.strptime(end_date_picker, "%Y-%m-%d") + datetime.timedelta(hours=int(end_time_picker[0:2]))
error_code = input_check_time(start_time, end_time)
#start_time = start_date_picker + " " + start_time_picker[0:2] + ":00"
#end_time = end_date_picker + " " + end_time_picker[0:2] + ":00"
if error_code:
df = pd.DataFrame(data=None)
df = df.to_json(date_format='iso', orient='split')
return None, None, None
parameters = [start_time, end_time]
print(parameters)
query_string = mssql_queries.build_db_query_string_app2()
records = mssql_conn.execute_query(query_string, parameters, db_name)
#query_string = mysql_queries.build_db_query_string_app2(db_name)
#records = connect.execute_query(query_string, parameters, db_name)
if not records:
return None, None, None
query_output = pd.DataFrame.from_records(records)
query_output.columns = ["ID", "Timestamp", "Type", "Result"]
if part_type != "all" or worker_name != "all":
if part_type != "all":
part_type_filter = utils.create_part_type_filter(db_name, parameters, part_type)
print(part_type_filter)
if part_type_filter.empty:
return None, None, None
query_output = utils.apply_filter(query_output, part_type_filter)
if worker_name != "all":
worker_filter = utils.create_worker_filter(db_name, parameters, worker_name)
if worker_filter.empty:
return None, None, None
query_output = utils.apply_filter(query_output, worker_filter)
if query_output.empty:
return None , None, None
categories = query_output.groupby("Result").count()
if categories["ID"][0] == False:
faulty_parts = categories["ID"][0]
ok_parts = categories["ID"][1]
else:
faulty_parts = 0
ok_parts = categories["ID"][0]
query_output["time_diff"] = 0
for i in range(query_output.shape[0] - 1):
query_output["time_diff"][i+1] = (query_output['Timestamp'][i+1] - query_output['Timestamp'][i]).total_seconds()
mean_cycle_time = round(query_output["time_diff"].mean(), 2)
return "OK Parts: {}".format(ok_parts) , "Faulty Parts: {}".format(faulty_parts), "Mean Cycle Time: {}".format(mean_cycle_time)
return None, None, None
And the sql query itself:
def build_db_query_string_app1(language):
db_query_string = """SELECT NotificationLog.FailureCode, FailureDescription.[Alarm text de-DE , Alarm text]text, NotificationLog.Duration, NotificationLog.Timestamp
FROM NotificationLog, FailureDescription
WHERE NotificationLog.Timestamp
BETWEEN ? AND ?
AND FailureDescription.ID = NotificationLog.FailureCode;""".format(language)
return db_query_string