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?
// 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!'