Problem with showing progress bar in realtime

I want make dynamically progression when user upload his pdf file in system.
My code:

ocr_read.py:

    size = len(pdf_image.sequence)
            for img in pdf_image.sequence:
                img_page = wi(image=img)
                image_blob = img_page.make_blob('jpeg')

                recognized_text = " "

                image = Image.open(io.BytesIO(image_blob))
                recognized_text = pytesseract.image_to_string(image, lang)
                text = text + recognized_text

                frappe.publish_realtime("ocr_progress_bar", {"progress": [i, size], "reload": 1}, user=frappe.session.user)
                i += 1

ocr_read.js:


frappe.ui.form.on('OCR Read', {
    refresh: function (frm) {

    },
    read_image: function (frm) {
        frappe.realtime.on("ocr_progress_bar", function (data) {
            frappe.hide_msgprint(true);
            if (data.data_import === frm.doc.name) {
                if (data.reload && data.reload === true) {
                    frm.reload_doc();
                }
            }
            frappe.call({
                method: "read_image",
                doc: cur_frm.doc,
                callback: function (r, rt) {
                    if (r.message) {
                        console.log(r.message);
                        cur_frm.set_value("read_result", r.message);
                        cur_dialog.hide()
                    }
                }
            });
            frappe.show_progress(__("Reading the file"), data.data_import[0], data.progress[1]);
        });
    }
});

But after when I press on read_image button nothing is happening

Try something like this:

JS:

frappe.ui.form.on('OCR Read', {
    refresh: function (frm) {},
    read_image: function (frm) {
        frappe.call({
            method: "path.to.ocr_read.read_image", // change this
            doc: cur_frm.doc,
            callback: function (r, rt) {
                if (r.message) {
                    console.log(r.message);
                    cur_frm.set_value("read_result", r.message);
                    cur_dialog.hide()
                }
            }
        });
    }
});

Python:

from frappe import publish_progress
import pytesseract

@frappe.whitelist()
def read_image():
    text = ""
    size = len(pdf_image.sequence)
    for i, img in enumerate(pdf_image.sequence):
        img_page = wi(image=img)
        image_blob = img_page.make_blob('jpeg')
        image = Image.open(io.BytesIO(image_blob))

        recognized_text = ""
        recognized_text = pytesseract.image_to_string(image, lang)
        text = text + recognized_text

        progress = i / size * 100
        publish_progress(percent=progress, title="Reading the file")
    
    return recognized_text
5 Likes

I tried this but it’s not working. Can you teach me how to do it?
:sweat_smile:

1 Like

Make sure, that you enabled socket.io on your server

1 Like

Thanks! It’s working now