简体   繁体   中英

How to pass data between running python process and javascript

I am working on a personal voice assistant app with python, and I want to add a web-interface to it, to allow using it remotely from any device. I already have a Python side of it, and I want to mix it with web interface. Maybe by listening for a socket in a thread. I am experienced with Python but not so much with JS.

What I ideally need is to have a running python process. Then HTML JS client records user's voice, converts it to text and sends it to the python server. Python server receives the command, and runs a function, and returns a json data like:

{
'reply':'All done, here is your result', 
'data':someBigHTMLoutput, 
'audio':path to generated mp3 audio file of speech using customized voice
}

Html can be a big table with found books or movies, for example, or some other report. Or it can be blank if the command was to turn off the light.

Then JS needs to receive this data, replace 'content' div with html data, display AI answer text and play tts audio file generated by the server. And wait for next command.

So, from all this I want to know how to approach js client and python server data exchange without page refresh?

Will it be enough to just listen for a socket with python? But then how do I return data back to JS?

And additional question is: Is it possible to push some data before and during function execution? Currently my Python voice assistant may sometimes 'say' things before, during and after function execution. Is there any way to send this sound to html page as well? If not I'll re-think the logic of AI answers to make it only reply after the 'work is done'.

I spent a lot of time googling this, but I only got confused with a huge choice of frameworks, while I am not even sure if I need any of them for this relatively simple task.

Thanks! Python 3.4

I ended up using Flask, Flask-SocketIO and JavaScript with socketio.

Works like a charm. Thanks. It allows me to emit events at any time, which makes it really easy to build a HTML+JS based front end UI, interacting in real time with python server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM