Skip to content

- Add recipe for selectolax#3352

Open
EchterAlsFake wants to merge 1 commit into
kivy:developfrom
EchterAlsFake:add-selectolax-recipe
Open

- Add recipe for selectolax#3352
EchterAlsFake wants to merge 1 commit into
kivy:developfrom
EchterAlsFake:add-selectolax-recipe

Conversation

@EchterAlsFake

Copy link
Copy Markdown

Hi,

I have added a recipe for the HTML5 parsing library selectolax. I have tested it alongside a QML application by importing the package and parsing a HTML structure.

The compilation aswell as the import worked fine without any errors.

Here's the exact code that I used for testing:

import sys
import os
import traceback
import asyncio
from PySide6.QtCore import QObject, Slot, Signal
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine

os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
os.environ["QT_QUICK_CONTROLS_MATERIAL_THEME"] = "Dark"
os.environ["QT_QUICK_CONTROLS_MATERIAL_ACCENT"] = "Blue"

class ScraperBridge(QObject):
    titleExtracted = Signal(str)

    @Slot()
    def startParsing(self):
        asyncio.create_task(self.async_extract())

    async def async_extract(self):

        sample_html = """
        <html>
            <head><title>Test Page</title></head>
            <body>
                <div class="content">
                    <h1>Hello from Selectolax & PySide6 QML!</h1>
                    <p>This parsing engine is running blazingly fast in C.</p>
                </div>
            </body>
        </html>
        """

        # Parse using selectolax's Lexbor engine
        from selectolax.lexbor import LexborHTMLParser
        tree = LexborHTMLParser(sample_html)
        h1_node = tree.css_first("div.content h1")

        if h1_node:
            extracted_text = h1_node.text(strip=True)
        else:
            extracted_text = "H1 Element not found!"

        # Send the clean string back to the QML interface
        self.titleExtracted.emit(extracted_text)



async def main():
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

   
    scraper_bridge = ScraperBridge()
    
    # Expose the Python object directly to the QML context engine
    engine.rootContext().setContextProperty("scraperBackend", scraper_bridge)

    engine.load("main.qml")

    if not engine.rootObjects():
        sys.exit(-1)

    while app.property("isExiting") is not True:
        app.processEvents()
        await asyncio.sleep(0.01)

if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant