admin管理员组文章数量:1405727
I am trying to make dashboard in flask by connecting it with SQL server and getting these errors. I confirm there are no null values and I checked by removing the column as well from query but still not working. Code is -
import pandas as pd
import pyodbc
from flask import Flask, render_template, jsonify
app = Flask(__name__)
# SQL Server Connection Details
conn_str = (
"DRIVER={SQL Server};"
"SERVER=xyz;"
"DATABASE=xyz;"
"UID=xyz;"
"PWD=xyz;"
)
# Fetch Data from SQL Server
def fetch_data():
try:
conn = pyodbc.connect(conn_str)
query = """
SELECT TicketDate, Technician, Open_Tickets, Closed_Tickets, Created_Today, Closed_Today, Created_Hourly
FROM Technician_Ticket_Stats
"""
df = pd.read_sql(query, conn)
conn.close()
# Debugging logs
print("Fetched data successfully:")
print(df.head())
df['TicketDate'] = df['TicketDate'].astype(str) # Convert date for JSON
return df.to_dict(orient="records")
except Exception as e:
print("Error fetching data:", e)
return []
@app.route("/")
def index():
return render_template("index.html")
@app.route("/get_data")
def get_data():
try:
data = fetch_data()
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host='127.0.0.1', port=8050, debug=True)here
I am trying to make dashboard in flask by connecting it with SQL server and getting these errors. I confirm there are no null values and I checked by removing the column as well from query but still not working. Code is -
import pandas as pd
import pyodbc
from flask import Flask, render_template, jsonify
app = Flask(__name__)
# SQL Server Connection Details
conn_str = (
"DRIVER={SQL Server};"
"SERVER=xyz;"
"DATABASE=xyz;"
"UID=xyz;"
"PWD=xyz;"
)
# Fetch Data from SQL Server
def fetch_data():
try:
conn = pyodbc.connect(conn_str)
query = """
SELECT TicketDate, Technician, Open_Tickets, Closed_Tickets, Created_Today, Closed_Today, Created_Hourly
FROM Technician_Ticket_Stats
"""
df = pd.read_sql(query, conn)
conn.close()
# Debugging logs
print("Fetched data successfully:")
print(df.head())
df['TicketDate'] = df['TicketDate'].astype(str) # Convert date for JSON
return df.to_dict(orient="records")
except Exception as e:
print("Error fetching data:", e)
return []
@app.route("/")
def index():
return render_template("index.html")
@app.route("/get_data")
def get_data():
try:
data = fetch_data()
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host='127.0.0.1', port=8050, debug=True)here
Share
edited Mar 7 at 11:18
EricLavault
16.2k3 gold badges27 silver badges56 bronze badges
asked Mar 7 at 10:49
UmeshUmesh
371 silver badge4 bronze badges
3
|
1 Answer
Reset to default 1Add this line so that if there are NaT values in the 'TicketDate' column, it converts to None rather than throwing an error.
df['TicketDate'] = df['TicketDate'].fillna(pd.NaT).apply(
lambda x: x.strftime('%Y-%m-%d') if pd.notna(x) else None
)
本文标签:
版权声明:本文标题:python - the server responded with a status of 500 (INTERNAL SERVER ERROR) and ValueError: NaTType does not support timetuple - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744935497a2633157.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
print()
(andprint(type(...))
,print(len(...))
, etc.) to see which part of code is executed and what you really have in variables. It is called"print debugging"
and it helps to see what code is really doing. – furas Commented Mar 7 at 14:33