Hi.
I’m very new to Dash development.
I am getting the aforementioned internal server error accompanied by the following in the browser:
Callback error updating ..login-url.pathname...login-alert.children..", html: '<!doctype html>\n<html lang=en>\n <head>\n <title>TypeError: Object of type builtin_function_or_method is not JSON serializable
And the following from the console logs:
127.0.0.1 - - [25/Jul/2024 17:32:08] "POST /_dash-update-component HTTP/1.1" 500 -
Traceback (most recent call last):
File "C:\Python312\Lib\site-packages\flask\app.py", line 1498, in __call__
return self.wsgi_app(environ, start_response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask\app.py", line 1476, in wsgi_app
response = self.handle_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask\app.py", line 1473, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask\app.py", line 883, in full_dispatch_request
return self.finalize_request(rv)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask\app.py", line 904, in finalize_request
response = self.process_response(response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask\app.py", line 1284, in process_response
self.session_interface.save_session(self, ctx.session, response)
File "C:\Python312\Lib\site-packages\flask\sessions.py", line 368, in save_session
val = self.get_signing_serializer(app).dumps(dict(session)) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\itsdangerous\serializer.py", line 316, in dumps
payload = want_bytes(self.dump_payload(obj))
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\itsdangerous\url_safe.py", line 56, in dump_payload
json = super().dump_payload(obj)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\itsdangerous\serializer.py", line 278, in dump_payload
return want_bytes(self.serializer.dumps(obj, **self.serializer_kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask\json\tag.py", line 323, in dumps
return dumps(self.tag(value), separators=(",", ":"))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask\json\__init__.py", line 41, in dumps
return current_app.json.dumps(obj, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask\json\provider.py", line 179, in dumps
return json.dumps(obj, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
^^^^^^^^^^^
File "C:\Python312\Lib\json\encoder.py", line 200, in encode
chunks = self.iterencode(o, _one_shot=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\json\encoder.py", line 258, in iterencode
return _iterencode(o, 0)
^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\flask\json\provider.py", line 121, in _default
raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable")
TypeError: Object of type builtin_function_or_method is not JSON serializable
Here is the callback code that I believe to be causing the trouble.
@dashApp.callback(
[
Output(component_id='login-url', component_property='pathname'),
Output(component_id='login-alert', component_property='children')
],
[
Input(component_id='login-button', component_property='n_clicks'),
Input(component_id='login-password', component_property='n_submit')
],
[
State('login-username', 'value'),
State('login-password', 'value')
],
prevent_initial_call=True
)
def login_success(n_clicks, n_submit, username, password):
if (n_submit and n_submit > 0) or (n_clicks and n_clicks > 0):
user = User.find_by_username(username)
if user and user.verify_password(password) == True:
login_user(user)
logger.info("Login success %s" % user)
return ('/home', success_alert)
else:
logger.info("Login error %s" % username)
return ('/logout', failure_alert)
else:
return no_update, ''
Can you please review the above and let me know if you spot an obvious issue with the code?
Its a multipage app that upon login, I want to transition to home page.
Thanks,
Ger.