[Python] How to rewrite python code using Pyscript

Discussão em 'Python' iniciado por Stack, Agosto 21, 2025.

  1. Stack

    Stack Membro Participativo

    My code works as python file but I am struggling to make it work using pyscript.I am sharing the code which I tried.

    main.py


    import pytesseract
    pytesseract.pytesseract.tesseract_cmd = r"Tesseract-OCR\tesseract.exe"
    list_img = []
    import os
    import cv2
    import pytesseract
    import pandas as pd
    list_img = []
    def fun_data(x):
    image = cv2.imread(list_img[x],0)
    thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
    data = "\n".join([ll.rstrip() for ll in data.splitlines() if ll.strip()])
    data = data.split('\n')
    return data
    def my_fun():
    directory = f'SQL_NOTES\\'
    file_names = os.listdir(directory)
    for file_name in file_names:
    if file_name.startswith("imagename"):
    list_img.append(directory + file_name)
    NumbersDict = dict({0 : 'list_img[0]', 1 : 'list_img[1]', 2 : 'list_img[2]', 3 : 'list_img[3]'})

    s = ([fun_data(a) for a in NumbersDict])
    return pd.DataFrame(s).T
    print(my_fun())

    index.html


    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Empty Grass</title>

    <!-- Recommended meta tags -->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">

    <!-- PyScript CSS -->
    <link rel="stylesheet" href="https://pyscript.net/releases/2025.7.3/core.css">

    <!-- This script tag bootstraps PyScript -->
    <script type="module" src="https://pyscript.net/releases/2025.7.3/core.js"></script>
    </head>
    <body>
    <button id="ex1">Introduction</button>
    <script type="py" config="./pyscript.toml" terminal>
    from pyscript import when
    @when("click", "#ex1")
    def click_handler(event):
    from main import my_fun
    my_fun()
    </script>
    </body>
    </html>

    pyscript.toml



    packages = [ "pytesseract","opencv-python","pandas"]

    [files]
    "main.py" = "main.py"


    The code extracts text from all images and will display like dataframe.I am mostly confused with rewriting pytesseract.pytesseract.tesseract_cmd = r"Tesseract-OCR\tesseract.exe" and directory = f'SQL_NOTES\'

    Continue reading...

Compartilhe esta Página