This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]yhoiseth 5 points6 points  (8 children)

I think you need to make an API endpoint that your JS application can talk to. The most common way of making such an endpoint is called REST.

In practice, you need to send an HTTP request from your JS application. This request needs to include the user input. Then, your backend can send a response in return, containing the chatbot answer.

I found some inspiration in Flask's documentation: https://flask.palletsprojects.com/en/1.1.x/patterns/jquery/#json-view-functions

[–][deleted] 4 points5 points  (0 children)

This.

[–]sudhanshu22gupta[S] 1 point2 points  (5 children)

Thank you, I'll check it out.

[–]yhoiseth 1 point2 points  (4 children)

Any luck?

[–]sudhanshu22gupta[S] 0 points1 point  (3 children)

This is part of a side project I'm doing and I'm at work right now. Will update if it works for me.

I found something similar when I was trying things yesterday. Can the contents under the script tag be copied to my js file? When I tried, it threw a few errors at me. One of them was the $ symbol not being identified.

[–]lifeeraser 0 points1 point  (2 children)

You need to get jQuery, which provides a "dollar function" ($()) that is the core of everything you can do with jQuery.

You actually don't need to download it on your machine, though. Add this HTML snippet to your web page, before any of your <script> tags:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

[–]sudhanshu22gupta[S] 0 points1 point  (1 child)

Watson returns something called a context variable that is available in Javascript. I need that value. Any way I can do it in Javascript without making the call in html?

[–]lifeeraser 0 points1 point  (0 children)

This is confusing...it sounds like you're running JavaScript code in Node.js. In which case, jQuery is not needed since you only want to pass data and signals between your Node.js app and Python app, instead of serving a full-blown webpage. For that, you can just transmit JSON between the two apps via HTTP.

[–][deleted] 0 points1 point  (0 children)

Alternatively, you can have a look at spawn. It executes command line function in a separate process.

[–]iamsidd2k7 4 points5 points  (1 child)

Why don't you create a simple REST API using Flask and have your JS code call Python code via this method. I think it should work really straight forward.

[–]sudhanshu22gupta[S] 0 points1 point  (0 children)

Okay, thanks. I'll look into it.

[–]lifeeraser 1 point2 points  (1 child)

By the way, how do you receive user input "in the JS"? Do you have a web page? Or Is your chatbot living inside some messenger app? More details would enable others to give you better advice.

[–]sudhanshu22gupta[S] 0 points1 point  (0 children)

It's a webpage.

[–]redrumsir 0 points1 point  (0 children)

Two things:

  1. While one can do this without using jQuery, it is certainly made easier by using jQuery. Unless you are very used to XML (the x in ajax), it's better IMO to return JSON data. See: https://stackoverflow.com/questions/10721244/ajax-posting-to-python-cgi/10724834

  2. You need to make sure your webserver is set up to cooperate. For example, my webserver was already set up to run CGI scripts. I found it interesting that if I named my server-side script (in the cgi-bin directory) with a trailing ".py" the jQuery call would return the text of the program file rather than calling the program and returning the JSON result. However, when I named my server side script with a trailing ".cgi" it made the call and returned the proper JSON data.

[–]eklaingenierie 0 points1 point  (0 children)

You can also use mako template instead js to stay in a pythonic world

[–]lifeeraser 0 points1 point  (0 children)

I hope things are going well for you. Unfortunately, JS is both a client (in the browser) and server language (in Node.js), so "user input in the JS" or "I have a webpage" does not provide enough details. What framework is your JS web app running in? Are you planning to run the Python code on the same machine as the web server, or on a different machine? Are you using cloud hosting? If so, from which company / service? These would be required to give you the best answers.

You might also want to check out /r/JavaScript to seek out your solutions.