Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call python script from javascript?

I have a backend python script where it retrieves the data from the sqlalchemy engine. And I would like to show the data in a search box where you can scroll down the list of data and select it. I read some answers to the similar questions like mine, (use ajax to call python script). But I'm still not clear about this. Here is my python script.

# models.py

from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
from sqlalchemy.ext.declarative import declarative_base
import pandas as pd

aURL = URL(drivername='mysql', username='chlee021690',  database = 'recommender')
engine = create_engine(aURL, echo=True)

sql_command = 'SELECT product_id FROM bestbuy_data'

results = pd.read_sql(sql = sql_command, con = engine)

Can anybody tell me how to create javscript code to retrieve that results and render it in my form? Thanks.

like image 205
user2585578 Avatar asked May 16 '26 04:05

user2585578


1 Answers

Step 1: make your script available as a web service. You can use CGI, or you can use one of the cool server frameworks that will run standalone or WSGI like CherryPy, web.py or Flask.

Step 2: make an AJAX call to the URL served by step 1, either manually (look for XmlHttpRequest examples), or easily using jQuery or another framework (jQuery.ajax(), jQuery.get()).

These are two separate tasks, both are well documented on the web. If you have a more specific question, I suggest you ask again, showing what you are stuck on.

There are also many examples for the complete package available ("python ajax example"), for example this.

like image 80
Amadan Avatar answered May 17 '26 17:05

Amadan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!