Checkin the dataframe querry for specific values and showing them in the report

Hello guys,

I made an app that takes records from a database and show’s them.Now I have several machines that use the same querry from the databases but the new one has one more value in itemlog in her own database, that the others don’t have. So I made an if - else function and it’s not reading the dataframes okay,do you maybe know what’s wrong here?
Or have some other suggestions?

Some notes:
I can pick 7 machines from a dropdown menu that read the same query as below and they give data.just the eight one has one more column in her ItemLog db.

I wanted to make something that read the querry and if there is no “FloatID1” in it it will show the version without it

Is it maybe a problem in my querry or I should do my function in some other way?
I put my old version of the function at the end below.

In here I tried to check if the column ‘Float1D’ is present in the returned records, and then use the correct code block.
Instead of trying to access the column in the first record, I use the pd.DataFrame.from_records() method to create a DataFrame from the records, and then check if the column ‘FloatID1’ is present in the DataFrame. If it is present, then it should the first code block, otherwise it should the second code block.

This is my current app.py

def show_users(
    n_clicks,
    start_date_picker,
    start_time_picker,
    end_date_picker,
    end_time_picker,
    database,
    result_selection,
    language,
):
    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]))
        parameters = [start_time, end_time]

        query_string = mssql_queries.build_db_query_string_app2_new(result_selection)
        records = mssql_conn.execute_query(query_string, parameters, str(database))
        print("result_selection")
        dataframe = pd.DataFrame.from_records(records)
        if 'FloatID1' in dataframe.columns:
            dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode", "FloatID1"]
            mask = dataframe.applymap(type) != bool
            d = {True: "1", False: "0"}
            dataframe = dataframe.where(mask, dataframe.replace(d))
            # Scrap Codes
            query_string = mssql_queries.build_db_query_string_defect_descriptions(language)
            records = mssql_conn.execute_without_params(query_string, str(database))
            scrap_codes = pd.DataFrame.from_records(records)
            scrap_codes.columns = ["DefectCode", "DefectDescription"]
            scrap_codes["DefectCode"]=scrap_codes["DefectCode"].apply(numpy.int64) 
            dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
        else:
            dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode"]
            mask = dataframe.applymap(type) != bool
            d = {True: "1", False: "0"}
            dataframe = dataframe.where(mask, dataframe.replace(d))
            # Scrap Codes
            query_string = mssql_queries.build_db_query_string_defect_descriptions(language)
            records = mssql_conn.execute_without_params(query_string, str(database))
            scrap_codes = pd.DataFrame.from_records(records)
            scrap_codes.columns = ["DefectCode", "DefectDescription"]
            scrap_codes["DefectCode"]=scrap_codes["DefectCode"].apply(numpy.int64) 
            dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
            print("----")
            print(dataframe)

            return (
                dbc.Table.from_dataframe(dataframe),
                dataframe.to_json(date_format="iso", orient="split"),
                None,
            )
        if records is None or len(records) == 0:
            return None, None, dbc.Alert("No entries within the specified timeframe.", color="warning", duration=5000)
    else:
        return dbc.Table(None), None, None

I just added ItemLog.FloatID1 in the query below,as I used this one without the ItemLog.FloatID1 before,maybe the placing is bad?

query.py

def build_db_query_string_app2_new(result_selection):
    if not result_selection:
        db_query_string = """SELECT ItemLog.ID, ItemLog.Timestamp, ItemLog.Type, ItemLog.Result, ItemLog.DefectCode, ItemLog.FloatID1
                            FROM ItemLog 
                            WHERE ItemLog.Timestamp BETWEEN ? AND ?"""
    else:
        db_query_string = """SELECT ItemLog.ID, ItemLog.Timestamp, ItemLog.Type, ItemLog.Result, ItemLog.DefectCode
                    FROM ItemLog 
                    WHERE ItemLog.Timestamp BETWEEN ? AND ? AND ItemLog.Result = {}""".format(result_selection)
    return db_query_string

if try it like this and pick a machine that doesn’t have " FloatID1" in his database then I get:

Traceback (most recent call last):
File “C:\Users\Administrator\Documents\dashboard\apps\app2.py”, line 169, in show_users
records = mssql_conn.execute_query(query_string, parameters, str(database))
File “C:\Users\Administrator\Documents\dashboard\mssql_conn.py”, line 63, in execute_query
cursor.execute(query_string, parameters)
pyodbc.ProgrammingError: (‘42S22’, “[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name ‘FloatID1’. (207) (SQLExecDirectW); [42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (8180)”)

If I try the machine that does have the " FloatID1" then I get:

ValueError: Length mismatch: Expected axis has 6 elements, new values have 5 elements

“C:\Users\Administrator\Documents\dashboard\apps\app2.py”, line 187, in show_users

dataframe.columns = [“ID”, “Timestamp”, “Type”, “Result”, “DefectCode”]

This is how my function in the app looked before the new value for the new machine:


def show_users(
    n_clicks,
    start_date_picker,
    start_time_picker,
    end_date_picker,
    end_time_picker,
    database,
    result_selection,
    language,
):
    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]))
        parameters = [start_time, end_time]

        query_string = mssql_queries.build_db_query_string_app2_new(result_selection)
        records = mssql_conn.execute_query(query_string, parameters, str(database))
        print("result_selection")
        # query_string = mysql_queries.build_db_query_string_app2(str(database))
        # records = connect.execute_query(query_string, parameters, str(database))
        if records:
            dataframe = pd.DataFrame.from_records(records)
            dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode"]

            mask = dataframe.applymap(type) != bool
            d = {True: "1", False: "0"}
            dataframe = dataframe.where(mask, dataframe.replace(d))

            # Scrap Codes
            query_string = mssql_queries.build_db_query_string_defect_descriptions(
                language
            )
            records = mssql_conn.execute_without_params(query_string, str(database))
            scrap_codes = pd.DataFrame.from_records(records)
            scrap_codes.columns = ["DefectCode", "DefectDescription"]

            scrap_codes["DefectCode"] = scrap_codes["DefectCode"].apply(numpy.int64)

            dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
            print("----")
            print(dataframe)

            return (
                dbc.Table.from_dataframe(dataframe),
                dataframe.to_json(date_format="iso", orient="split"),
                None,
            )
        else:
            return (
                None,
                None,
                dbc.Alert(
                    "No entries within the specified timeframe.",
                    color="warning",
                    duration=5000,
                ),
            )
    else:
        return dbc.Table(None), None, None

Hello @Traxdata,

It looks like your function that is firing before you use the if statement is the problem.

Id bring it under your if statement for your columns.

Hi thank you for your replay,
can you please explain a bit more about you last sentence? I didn’t understand it very well.

I’m on my phone, so it’s a little hard, but…

Where you have query_string = … before that into your if statement, and pass whether or not to include the additional column in the sql statement.

I did try to add it like this:
with column_include, but I think I missed the point.Could be that I’m overthinking this.

     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]))
        parameters = [start_time, end_time]
        column_include = True
        # query_string_suso = build_db_query_string_app2_suso(result_selection)
        query_string = mssql_queries.build_db_query_string_app2_new(column_include,result_selection)
        records = mssql_conn.execute_query(query_string, parameters, str(database))
        print("result_selection")
        dataframe = pd.DataFrame.from_records(records)
        # query_string = mysql_queries.build_db_query_string_app2(str(database))
        # records = connect.execute_query(query_string, parameters, str(database))
        # if database == "GSL-2 SUSO":
        #     query_string_suso = build_db_query_string_app2_suso()
        # else:
        if column_include:
            query_string = mssql_queries.build_db_query_string_app2_new(True,result_selection)
            dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode", "FloatID1"]
            mask = dataframe.applymap(type) != bool
            d = {True: "1", False: "0"}
            dataframe = dataframe.where(mask, dataframe.replace(d))
            # Scrap Codes
            query_string = mssql_queries.build_db_query_string_defect_descriptions(language)
            records = mssql_conn.execute_without_params(query_string, str(database))
            scrap_codes = pd.DataFrame.from_records(records)
            scrap_codes.columns = ["DefectCode", "DefectDescription"]
            scrap_codes["DefectCode"]=scrap_codes["DefectCode"].apply(numpy.int64) 
            dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
            print("Value found: ", records)
        else:
            query_string = mssql_queries.build_db_query_string_app2_new(False,result_selection)
            dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode"]
            mask = dataframe.applymap(type) != bool
            d = {True: "1", False: "0"}
            dataframe = dataframe.where(mask, dataframe.replace(d))
            # Scrap Codes
            query_string = mssql_queries.build_db_query_string_defect_descriptions(language)
            records = mssql_conn.execute_without_params(query_string, str(database))
            scrap_codes = pd.DataFrame.from_records(records)
            scrap_codes.columns = ["DefectCode", "DefectDescription"]
            scrap_codes["DefectCode"]=scrap_codes["DefectCode"].apply(numpy.int64) 
            dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
            print("----")
            print(dataframe)

Hello so what I managed to do is to make the code like this:
Thsi is working fine as when I pick machine that aren’t using the GSL-2 databse it’s working fine,but when I switch to the GSL-2 I’m getting the following erros:

Do you have any tips for what I’m messing up?
Thank you

Traceback (most recent call last):
** File “C:\Users\Administrator\Documents\dashboard\jifeng_reporting\apps\app2.py”, line 178, in show_users**
** dataframe.columns = [“ID”, “Timestamp”, “Type”, “Result”, “DefectCode”, “FloatID1”]**
** File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\pandas\core\generic.py”, line 5473, in setattr**
** return object.setattr(self, name, value)**
** File “pandas_libs\properties.pyx”, line 66, in pandas._libs.properties.AxisProperty.set**

** File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\pandas\core\generic.py”, line 667, in _set_axis**
** self._mgr.set_axis(axis, labels)**
** File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\pandas\core\internals\managers.py”, line 220, in set_axis**
** raise ValueError(**
ValueError: Length mismatch: Expected axis has 5 elements, new values have 6 element

def show_users(
    n_clicks,
    start_date_picker,
    start_time_picker,
    end_date_picker,
    end_time_picker,
    database,
    result_selection,
    language,
):
    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]))
        parameters = [start_time, end_time]

        query_string = mssql_queries.build_db_query_string_app2_suso
        query_string = mssql_queries.build_db_query_string_app2_new(result_selection)
        records = mssql_conn.execute_query(query_string, parameters, str(database))
        print("result_selection")
        # dataframe = pd.DataFrame.from_records(records)
        # query_string = mysql_queries.build_db_query_string_app2(str(database))
        # records = connect.execute_query(query_string, parameters, str(database))
        if database == "GSL-2 SUSO":
            query_string = mssql_queries.build_db_query_string_app2_suso
            dataframe = pd.DataFrame.from_records(records)
            dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode", "FloatID1"]

            mask = dataframe.applymap(type) != bool
            d = {True: "1", False: "0"}
            dataframe = dataframe.where(mask, dataframe.replace(d))

            # Scrap Codes
            query_string = mssql_queries.build_db_query_string_defect_descriptions(language)
            records = mssql_conn.execute_without_params(query_string, str(database))
            scrap_codes = pd.DataFrame.from_records(records)
            scrap_codes.columns = ["DefectCode", "DefectDescription"]

            scrap_codes["DefectCode"]=scrap_codes["DefectCode"].apply(numpy.int64)
 
            dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
            print("Value found: ", records)
        elif records:
                    dataframe = pd.DataFrame.from_records(records)
                    dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode"]
        
                    mask = dataframe.applymap(type) != bool
                    d = {True: "1", False: "0"}
                    dataframe = dataframe.where(mask, dataframe.replace(d))
        
                    # Scrap Codes
                    query_string = mssql_queries.build_db_query_string_defect_descriptions(
                        language
                    )
                    records = mssql_conn.execute_without_params(query_string, str(database))
                    scrap_codes = pd.DataFrame.from_records(records)
                    scrap_codes.columns = ["DefectCode", "DefectDescription"]
        
                    scrap_codes["DefectCode"] = scrap_codes["DefectCode"].apply(numpy.int64)
        
                    dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
                    print("----")
                    print(dataframe)
        
                    return (
                        dbc.Table.from_dataframe(dataframe),
                        dataframe.to_json(date_format="iso", orient="split"),
                        None,
                    )
        if  records is None or len(records) == 0:
            return None, None, dbc.Alert("No entries within the specified timeframe.", color="warning", duration=5000)
    else:
        return dbc.Table(None), None, None, None

My query:

def build_db_query_string_app2_suso():
    db_query_string = """SELECT ItemLog.ID, ItemLog.Timestamp, ItemLog.Type, ItemLog.Result, ItemLog.FloatID1
                         FROM ItemLog 
                         WHERE ItemLog.Timestamp BETWEEN ? AND ?"""
    return db_query_string

A test I did to see the values:

Hello @Traxdata,

Try using this instead:

def show_users(
        n_clicks,
        start_date_picker,
        start_time_picker,
        end_date_picker,
        end_time_picker,
        database,
        result_selection,
        language,
):
    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]))
        parameters = [start_time, end_time]
        # 
        query_string = mssql_queries.build_db_query_string_app2_suso
        query_string = mssql_queries.build_db_query_string_app2_new(result_selection)
        records = mssql_conn.execute_query(query_string, parameters, str(database))
        print("result_selection")

        if database == "GSL-2 SUSO":
            query_string = mssql_queries.build_db_query_string_app2_suso
            dataframe = pd.read_sql_query(query_string, mssql_conn, params=parameters)
            dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode", "FloatID1"]

            mask = dataframe.applymap(type) != bool
            d = {True: "1", False: "0"}
            dataframe = dataframe.where(mask, dataframe.replace(d))

            # Scrap Codes
            query_string = mssql_queries.build_db_query_string_defect_descriptions(language)
            records = mssql_conn.execute_without_params(query_string, str(database))
            scrap_codes = pd.DataFrame.from_records(records)
            scrap_codes.columns = ["DefectCode", "DefectDescription"]

            scrap_codes["DefectCode"] = scrap_codes["DefectCode"].apply(numpy.int64)

            dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
            print("Value found: ", records)
        elif records:
            query_string = mssql_queries.build_db_query_string_app2_suso
            dataframe = pd.read_sql_query(query_string, params=parameters, mssql_conn)
            dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode"]

            mask = dataframe.applymap(type) != bool
            d = {True: "1", False: "0"}
            dataframe = dataframe.where(mask, dataframe.replace(d))

            # Scrap Codes
            query_string = mssql_queries.build_db_query_string_defect_descriptions(
                language
            )
            records = mssql_conn.execute_without_params(query_string, str(database))
            scrap_codes = pd.DataFrame.from_records(records)
            scrap_codes.columns = ["DefectCode", "DefectDescription"]

            scrap_codes["DefectCode"] = scrap_codes["DefectCode"].apply(numpy.int64)

            dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
            print("----")
            print(dataframe)

            return (
                dbc.Table.from_dataframe(dataframe),
                dataframe.to_json(date_format="iso", orient="split"),
                None,
            )
        if records is None or len(records) == 0:
            return None, None, dbc.Alert("No entries within the specified timeframe.", color="warning", duration=5000)
    else:
        return dbc.Table(None), None, None, None

You’re also querying the database a lot, would be better to only query when needed. :slight_smile:

Hi,
Thank you for the reply.
Yes sometimes I just write too much,the same things all again,it’s one of my downsides haha.
But when I try your code I’m getting:
You familiar with these?

AttributeError: module ‘mssql_conn’ has no attribute ‘cursor’

File "C:\Users\Administrator\apps\app2.py", line 175, in show_users

dataframe = pd.read_sql_query(query_string, mssql_conn, params=parameters)

File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\pandas\io\sql.py", line 377, in read_sql_query

return pandas_sql.read_query(

File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\pandas\io\sql.py", line 1743, in read_query

cursor = self.execute(*args)

File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\pandas\io\sql.py", line 1695, in execute

cur = self.con.cursor()

And also:

dash.exceptions.InvalidCallbackReturnValue: Invalid number of output values for …report_check_criteria.children…excel_data2.data…alert2.children…
Expected 3, got 4
Traceback (most recent call last):
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py”, line 1950, in full_dispatch_request
rv = self.dispatch_request()
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py”, line 1936, in dispatch_request
return self.view_functionsrule.endpoint
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\dash_auth\basic_auth.py”, line 33, in wrap
response = f(*args, **kwargs)
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\dash\dash.py”, line 1076, in dispatch
response.set_data(func(*args, outputs_list=outputs_list))
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\dash\dash.py”, line 1017, in add_context
_validate.validate_multi_return(output_spec, output_value, callback_id)
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\dash_validate.py”, line 126, in validate_multi_return
raise exceptions.InvalidCallbackReturnValue(
dash.exceptions.InvalidCallbackReturnValue: Invalid number of output values for …report_check_criteria.children…excel_data2.data…alert2.children…
Expected 3, got 4

Hmm, replace mssql_conn with whatever the name is of your sql connection.

I was assuming it was that because of the naming. :wink:

Hi,
seems that is not the problem but I’m missing the point of the “result_selection”. This indicates that the the query should include the field “result” from the database. So I need two query strings: One where result is included and one where it is not. I think nested if-else control structures would be best:
Could you help how would that look with the parameters below?


If db_name = “GSL-2 SUSO”:
                If result_selection == …:
                               # Build db query string where result and the float field are included
                Else:
                               # Build query string with just the float
Else:
                If result_selection
                               # old query string with result filter
                Else:
                               # old query string without result filter

Based on this I also need different lists for the columns (dataframe.columns = …)
Here are my parameters:


        query_string_suso = mssql_queries.build_db_query_string_app2_suso

        def build_db_query_string_app2_suso():
        db_query_string = """SELECT ItemLog.ID, ItemLog.Timestamp, ItemLog.Type, ItemLog.Result, ItemLog.FloatID1
                             FROM ItemLog 
                             WHERE ItemLog.Timestamp BETWEEN ? AND ?"""
        return db_query_string

        dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode", "FloatID1"]



        query_string = mssql_queries.build_db_query_string_app2_new(result_selection)

        def build_db_query_string_app2_new(result_selection):
            if not result_selection:
                db_query_string = """SELECT ItemLog.ID, ItemLog.Timestamp, ItemLog.Type, ItemLog.Result, ItemLog.DefectCode
                                    FROM ItemLog 
                                    WHERE ItemLog.Timestamp BETWEEN ? AND ?"""
            else:
                db_query_string = """SELECT ItemLog.ID, ItemLog.Timestamp, ItemLog.Type, ItemLog.Result, ItemLog.DefectCode
                            FROM ItemLog 
                            WHERE ItemLog.Timestamp BETWEEN ? AND ? AND ItemLog.Result = {}""".format(
                    result_selection
                )
            return db_query_string

        dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode"]

        records = mssql_conn.execute_query(query_string, parameters, str(database))

Yes.

Your query string must return the number of columns that you are trying to overwrite. I am confused as to why you are even trying to overwrite them though. As the columns would already have these names…

I feel like you are doing a lot of work for something simple.

For example, you could have your base select as a separate variable. If db2 then you just add the extra selecting columns to that variable.

Then query from the db.

Hi,

In my mssql_qeries.py

I made a new sql query.
Do you think that I don’t need the
if db_name == “GSL-2 SUSO”:
in my query?
I just need to call build_db_query_string_app2_suso accordingly in the function from the app now.

def build_db_query_string_app2_suso(db_name, result_selection):
    if db_name == "GSL-2 SUSO":
        if result_selection:
            db_query_string = """SELECT ItemLog.ID, ItemLog.Timestamp, ItemLog.Type, ItemLog.Result, ItemLog.FloatID1
                                 FROM ItemLog 
                                 WHERE ItemLog.Timestamp BETWEEN ? AND ? AND ItemLog.Result = {}""".format(
                result_selection
            )
        else:
            db_query_string = """SELECT ItemLog.ID, ItemLog.Timestamp, ItemLog.Type, ItemLog.FloatID1
                                 FROM ItemLog 
                                 WHERE ItemLog.Timestamp BETWEEN ? AND ?"""
    else:
        if result_selection:
            db_query_string = """SELECT ItemLog.ID, ItemLog.Timestamp, ItemLog.Type, ItemLog.Result, ItemLog.DefectCode
                                 FROM ItemLog 
                                 WHERE ItemLog.Timestamp BETWEEN ? AND ? AND ItemLog.Result = {}""".format(
                result_selection
            )
        else:
            db_query_string = """SELECT ItemLog.ID, ItemLog.Timestamp, ItemLog.Type, ItemLog.DefectCode
                                 FROM ItemLog 
                                 WHERE ItemLog.Timestamp BETWEEN ? AND ?"""
    return db_query_string

I added it like this but after the two elses in example the dataframes in the comment:

                #dataframe.columns = ["ID", "Timestamp", "Type", "FloatID1"]

It gives out again the:

Traceback (most recent call last):
File “C:\Users\Administrator\Documents\dashboard\jifeng_reporting\apps\app2.py”, line 212, in show_users
dataframe.columns = [“ID”, “Timestamp”, “Type”, “FloatID1”]
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\pandas\core\generic.py”, line 5473, in setattr
return object.setattr(self, name, value)
File “pandas_libs\properties.pyx”, line 66, in pandas._libs.properties.AxisProperty.set

File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\pandas\core\generic.py”, line 667, in _set_axis
self._mgr.set_axis(axis, labels)
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Lib\site-packages\pandas\core\internals\managers.py”, line 220, in set_axis
raise ValueError(
ValueError: Length mismatch: Expected axis has 5 elements, new values have 4 elements

ValueError: Length mismatch: Expected axis has 5 elements, new values have 4 elements

        if database == "GSL-2 SUSO":
            if result_selection:
                dataframe = pd.DataFrame.from_records(records)
                dataframe.columns = ["ID", "Timestamp", "Type", "Result", "FloatID1"]
                
                # Scrap Codes
                query_string = mssql_queries.build_db_query_string_defect_descriptions(
                    language
                )
                records = mssql_conn.execute_without_params(query_string, str(database))
                scrap_codes = pd.DataFrame.from_records(records)
                scrap_codes.columns = ["DefectCode", "DefectDescription"]
            
                scrap_codes["DefectCode"] = scrap_codes["DefectCode"].apply(numpy.int64)
            
                dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
                print("----")
                print(dataframe)
            else:
                dataframe = pd.DataFrame.from_records(records)
                #dataframe.columns = ["ID", "Timestamp", "Type", "FloatID1"]
    
                mask = dataframe.applymap(type) != bool
                d = {True: "1", False: "0"}
                dataframe = dataframe.where(mask, dataframe.replace(d))
    
                # Scrap Codes
                query_string = mssql_queries.build_db_query_string_defect_descriptions(
                    language
                )
                records = mssql_conn.execute_without_params(query_string, str(database))
                scrap_codes = pd.DataFrame.from_records(records)
                scrap_codes.columns = ["DefectCode", "DefectDescription"]
    
                scrap_codes["DefectCode"] = scrap_codes["DefectCode"].apply(numpy.int64)
    
                dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
                print("----")
                print(dataframe)
        else:
            if result_selection:
                dataframe = pd.DataFrame.from_records(records)
                dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode"]

                mask = dataframe.applymap(type) != bool
                d = {True: "1", False: "0"}
                dataframe = dataframe.where(mask, dataframe.replace(d))

                # Scrap Codes
                query_string = mssql_queries.build_db_query_string_defect_descriptions(
                    language
                )
                records = mssql_conn.execute_without_params(query_string, str(database))
                scrap_codes = pd.DataFrame.from_records(records)
                scrap_codes.columns = ["DefectCode", "DefectDescription"]

                scrap_codes["DefectCode"] = scrap_codes["DefectCode"].apply(numpy.int64)

                dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
                print("----")
                print(dataframe)
            else:
                dataframe = pd.DataFrame.from_records(records)
                #dataframe.columns = ["ID", "Timestamp", "Type", "DefectCode"]

                mask = dataframe.applymap(type) != bool
                d = {True: "1", False: "0"}
                dataframe = dataframe.where(mask, dataframe.replace(d))

                # Scrap Codes
                query_string = mssql_queries.build_db_query_string_defect_descriptions(
                    language
                )
                records = mssql_conn.execute_without_params(query_string, str(database))
                scrap_codes = pd.DataFrame.from_records(records)
                scrap_codes.columns = ["DefectCode", "DefectDescription"]

                scrap_codes["DefectCode"] = scrap_codes["DefectCode"].apply(numpy.int64)

                dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
                print("----")
                print(dataframe)
        if  records is None or len(records) == 0:
            return None, None, dbc.Alert("No entries within the specified timeframe.", color="warning", duration=5000)
    else:
        return dbc.Table(None), None, None

Here, this is what my thinking is for query function:

def build_db_query_string_app2_suso(db_name, result_selection):
    base_query = " FROM ItemLog WHERE ItemLog.Timestamp BETWEEN ? AND ?"
    columns_query = "SELECT ItemLog.ID, ItemLog.Timestamp, ItemLog.Type, ItemLog.Result"
    if db_name == "GSL-2 SUSO":
        columns_query += ", ItemLog.FloatID1"
        
    if result_selection:
        base_query += """AND ItemLog.Result = {}""".format(result_selection)

    return columns_query + base_query

Then try this in your callback:

def show_users(
        n_clicks,
        start_date_picker,
        start_time_picker,
        end_date_picker,
        end_time_picker,
        database,
        result_selection,
        language,
):
    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]))
        parameters = [start_time, end_time]

        query_string = mssql_queries.build_db_query_string_app2_new(result_selection)
        records = mssql_conn.execute_query(query_string, parameters, str(database))
        print("result_selection")

        if database == "GSL-2 SUSO":
            query_string = mssql_queries.build_db_query_string_app2_suso(database, result_selection)
            records = mssql_conn.execute_query(query_string, parameters, str(database))
            dataframe = pd.DataFrame.from_records(records)
            dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode", "FloatID1"]

            mask = dataframe.applymap(type) != bool
            d = {True: "1", False: "0"}
            dataframe = dataframe.where(mask, dataframe.replace(d))

            # Scrap Codes
            query_string = mssql_queries.build_db_query_string_defect_descriptions(language)
            records = mssql_conn.execute_without_params(query_string, str(database))
            scrap_codes = pd.DataFrame.from_records(records)
            scrap_codes.columns = ["DefectCode", "DefectDescription"]

            scrap_codes["DefectCode"] = scrap_codes["DefectCode"].apply(numpy.int64)

            dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
            print("Value found: ", records)
        elif records:
            dataframe = pd.DataFrame.from_records(records)
            dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode"]

            mask = dataframe.applymap(type) != bool
            d = {True: "1", False: "0"}
            dataframe = dataframe.where(mask, dataframe.replace(d))

            # Scrap Codes
            query_string = mssql_queries.build_db_query_string_defect_descriptions(
                language
            )
            records = mssql_conn.execute_without_params(query_string, str(database))
            scrap_codes = pd.DataFrame.from_records(records)
            scrap_codes.columns = ["DefectCode", "DefectDescription"]

            scrap_codes["DefectCode"] = scrap_codes["DefectCode"].apply(numpy.int64)

            dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
            print("----")
            print(dataframe)

            return (
                dbc.Table.from_dataframe(dataframe),
                dataframe.to_json(date_format="iso", orient="split"),
                None,
            )
        if records is None or len(records) == 0:
            return None, None, dbc.Alert("No entries within the specified timeframe.", color="warning", duration=5000)
    else:
        return dbc.Table(None), None, None, None

Hi,
Thank you for all help!

So the problem is that I forgot about the Return statement after the first if,so that is why it didn’t work.

Here is what I have been missing:

            return (dbc.Table.from_dataframe(dataframe), dataframe.to_json(date_format="iso", orient="split"), None)

Here is the code that is working:

if database == "GSL-2 SUSO":
            query_string = mssql_queries.build_db_query_string_app2_suso_old(database, result_selection)
            records = mssql_conn.execute_query(query_string, parameters, str(database))
            dataframe = pd.DataFrame.from_records(records)
            dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode", "FloatID1"]
            
            
        
            mask = dataframe.applymap(type) != bool
            d = {True: "1", False: "0"}
            dataframe = dataframe.where(mask, dataframe.replace(d))
        
            # Scrap Codes
            query_string = mssql_queries.build_db_query_string_defect_descriptions(language)
            records = mssql_conn.execute_without_params(query_string, str(database))
            scrap_codes = pd.DataFrame.from_records(records)
            scrap_codes.columns = ["DefectCode", "DefectDescription"]
        

            scrap_codes["DefectCode"] = scrap_codes["DefectCode"].apply(numpy.int64)
            dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")
            return (dbc.Table.from_dataframe(dataframe), dataframe.to_json(date_format="iso", orient="split"), None)
        elif records:
            dataframe = pd.DataFrame.from_records(records)
dataframe["FloatID1"] = None komentiran
            dataframe.columns = ["ID", "Timestamp", "Type", "Result", "DefectCode"]

            mask = dataframe.applymap(type) != bool
            d = {True: "1", False: "0"}
            dataframe = dataframe.where(mask, dataframe.replace(d))

            # Scrap Codes
            query_string = mssql_queries.build_db_query_string_defect_descriptions(language)
            
            
            records = mssql_conn.execute_without_params(query_string, str(database))
            scrap_codes = pd.DataFrame.from_records(records)
            scrap_codes.columns = ["DefectCode", "DefectDescription"]

            scrap_codes["DefectCode"] = scrap_codes["DefectCode"].apply(numpy.int64)

            dataframe = dataframe.merge(scrap_codes, how="left", on="DefectCode")


            return (
                dbc.Table.from_dataframe(dataframe),
                dataframe.to_json(date_format="iso", orient="split"),
                None,
            )
        if records is None or len(records) == 0:
            return None, None, dbc.Alert("No entries within the specified timeframe.", color="warning", duration=5000)
    else:
        return dbc.Table(None), None, None

Glad you got something to work.

I still think you are doing extra work by repeating all the queries instead of just building it an executing it once. :slight_smile: