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
  • always put full error message because there are other useful information. And put it in question, not in title. – furas Commented Mar 7 at 14:32
  • Maybe first use print() (and print(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
  • if you would remove try/except for some time then you would see full error with more details. – furas Commented Mar 7 at 14:34
Add a comment  | 

1 Answer 1

Reset to default 1

Add 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
)

本文标签: