Run python file from webform

Dear All,

I have a python file on my server now I want to run this file on click of a button , and this button is present on webform client script.

I tried ajax call but didn’t work. Any solution?

Regards,
Vishakha

Did you try this?

yes, but i am thinking there is some mistake in my code, can you give some code example?

Please post your code here.

frappe.web_form.after_load = () => {

    let data        =  frappe.web_form.get_values();
    var name        =  data.first_name
    var email       =  data.contact_no
    var contact_no  =  data.email_address
    var password    =  data.set_password

    document.getElementById("submitButton").onclick = function () {
       console.log("It is happening..")
        
       frappe.call({
        type: "POST",   
        url: 'mywebsite.com/home/frappe/frappe-bench/apps/frappe/PyhtonScript.py',
        callback: function(r) {
            console.log("repsonse",r)
        }
    });
    };

}

I think may be i am give url in wrong way,my file is on “mywebsite.com/home/frappe/frappe-bench/apps/frappe/PythonScript.py” location.

You need a whitelisted method inside a python file, inside a bench app, that is installed in your site.

For example, you can call the method ping of the app frappe, that is located in the file ~/frappe-bench/apps/frappe/frappe/__init__.py:

@frappe.whitelist()
def ping():
	return "pong"

You can call ping from the JavaScript console of your browser:

frappe.call({method: 'frappe.ping', callback: (r) => console.log(r)})
/* >> {message: "pong"} */

In your case, you would create a new app

# go to bench directory
cd ~/frappe-bench
# create a new app "my_app"
bench new-app my_app
# install "my_app" into your site
bench install my_app

and move your PyhtonScript.py to ~/frappe-bench/apps/my_app/my_app/my_script.py. Suppose your PyhtonScript.py has this content:

import frappe

@frappe.whitelist()
def ping():
	return "pong"

then you can call the method ping from JavaScript like this:

frappe.call({ method: 'my_app.my_script.ping', callback: (r) => console.log(r)})
/* >> {message: "pong"} */
1 Like

Hi Thanks for this solution but i am getting 504 (Timeout while reading response from Server) error while doing ajax call exactly the way defined.
The python script is runiing but before complition this error occures.
Any idea on this?

My js code:
frappe.web_form.after_load = () => {

    document.getElementById("submitButton").onclick = function () {
        let data        =  frappe.web_form.get_values();
        var name        =  data.first_name
        var email       =  data.contact_no
        var contact_no  =  data.email_address
        var password    =  data.set_password
 
       frappe.call({
            args: {
                'site_name'       : name,
                'admin_password' : password 
            },
            method: "frappe.init_new_frappe_site.create_new_site", //dotted path to server method
            callback: function(r) {
                console.log("Response:",r)
            }
       });
}

}

my py code:
import frappe
import subprocess
import os
import pexpect
import logging

@frappe.whitelist(allow_guest=True)
def myfunction(site_name,admin_password):

    //mylogic
    return data

Regards,
Vishakha