简体   繁体   中英

How to run a specific Javascript function using python

I have a Javascript file with 2 functions eg

function function1(){
console.log("Hello world")
}

function function2(){
console.log("Hello world number 2")
}

And I want to have a python function which executes either one of these function, how would I do this? BTW I want to do this as I will make one of my python scripts run and generate a value and I want to use Javascript to display this value onto the HTML file fancily.

This is possible via setting up Python's backend - then you can render frontend using proper HTML files with specified JS functionality.

You could use Flask in order to handle the backend in Python:

(remember to install flask before you use it pip3 install flask )

from flask import Flask, render_template
app = Flask(__name__, template_folder= "/templates")

@app.route('/function1')
def function1():
    return render_template('function1.html')

@app.route('/function2')
def function2():
    return render_template('function2.html')

app.run(port = 8000)

(The templates folder contains different websites you want to display - the ones with your JS functionalities)

Now when you open your browser with localhost:8000/function1 You should see your website showing evaluated function1 value

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