How to program a button field type using server side?

Greetings, everyone :wave:

I have a doctype named Google Translator. Inside this doctype, I added a button field type named btn. Then, I program it to do a task using client side script:
frappe.ui.form.on(“Google Translator”, “btn”, function (frm) {});

And it worked fine.

My question here is, how to accomplish this task using Python (server side script) ?
In other words, what is the python equivalent code of the the above JS code?

Regards,

Write a python method (it could be whitelisted) and call the method from inside the js script on callback perform needed client tasks

1 Like

I’m reading about frapp.call
Thank you

It works perfectly :grin:

.js Code:

// Omar M Shehada
frappe.ui.form.on('Google Translator', {
	refresh: function(frm) {
		frm.set_intro('Enter a text -> Pick up a language to translate to -> Hit Do Translate! -------> **You need to be ONLINE**')

		// Making edits on the css level
        set_css(frm);
	},

	btn: function(frm) {
		frm.call({
			doc: frm.doc,
			method: 'frm_call',
			args: {
				msg: 'Hallo!'
			},
			freeze: true,
			freeze_message: __("Translating..."),
			callback: function(r) {
				frappe.msgprint(r.message)
			}
		});
	}

});


// Making edits on the css level
function set_css(frm) {
  document.querySelectorAll("[data-fieldname='text']")[1].style.color = '#202020'
  document.querySelectorAll("[data-fieldname='text']")[1].style.fontWeight = 'bold'
  document.querySelectorAll("[data-fieldname='text']")[1].style.background = '#12ff21'
}

.py Code:

import frappe
from frappe import _
from frappe.model.document import Document
from googletrans import Translator

class GoogleTranslator(Document):
	# Keep in mind that this doctype needs to be connected to the Internet
	@frappe.whitelist()
	def frm_call(self, msg):
		import time
		time.sleep(0.5)
		trans = Translator()
		text = self.text

		try:
			out = trans.translate(text, dest=self.pick_a_lang[:2])
			self.translated_text = out.text
		except:
			self.translated_text = 'It works. Try it!'

Result:

4 Likes

Hello @Omar_M_K_Shehada

In which file did you place the python code?