Well, I can't see your problem immediately, but you've not made it easy for anyone to help you.
Am I going to retype your screen images to replicate the problem? No. Am I going to write a main() function which wraps your code, and then work out what all the import statements need to be to make it complete? No.
Therefore, I suggest the best way to get help is to produce a standalone application which replicates the problem, and submit it in *text* format, so it's something we can simply copy-paste into a terminal, run and debug.
It doesn't need the HTML or Javascript parts - it's fine to use 'curl -X POST ...' to send a message to it.
Brian.