Skip to content

Conversation

@ethan-0l
Copy link

@ethan-0l ethan-0l commented Feb 3, 2025

Hello

Was trying with Sanic web framework and complaining about some missing str methods.

This is a very naive "fix" might not be the best but streaming html works with this.

@ethan-0l ethan-0l marked this pull request as draft February 3, 2025 03:44
@tomasr8
Copy link
Owner

tomasr8 commented Feb 3, 2025

Hi! Can you give an example of how you are using it with Sanic? Wouldn't it be possible to simply call str(some_jsx) which gives you a string and you can stream that?

@ethan-0l
Copy link
Author

ethan-0l commented Feb 3, 2025

Actually yes i can, but since i saw that the type checking is

JSX: TypeAlias = JSXElement | str

I thought that it could be great to work like str and less misleading, maybe for other case too, and gave it a try. It appears that the send() wait bytes so i added direct encoding in the draft

Actually, I just made a dummy test site to explore. I'm use to jinja in many tools but though this can be way more interesting.

@bp.route("/")
async def index(request):
    """Route principale avec streaming HTML."""
    try:
        request.app.ctx.state["visitor_count"] += 1

        response = await request.respond() #content_type="text/html; charset=utf-16")

        home = HomePage(visitor_count=request.app.ctx.state["visitor_count"])
        await response.send(home)
        await response.eof()

    except Exception as e:
        logger.error(f"Erreur lors du streaming HTML : {e}")
        return HTTPResponse("Internal Server Error", status=500)
# ---
def HomePage(visitor_count: int = 0) -> JSX:
    return (
        <BaseLayout title="Accueil">
            <Header />
            <main style={{"padding": "1rem"}}>
                <div id="content">
                    <p>Contenu initial chargé en streaming.</p>
                    <div id="dynamic-container">
                        <Article id="1" />
                    </div>
                </div>
            </main>
            <Footer visitor_count={visitor_count} />
        </BaseLayout>
    )

By the way, it's just a proposition, if you think of a better way or that shouldn't work this way...

@tomasr8
Copy link
Owner

tomasr8 commented Feb 3, 2025

Thanks for the example! What is the error you get? Do you actually need all of the dunder methods?

@ethan-0l
Copy link
Author

ethan-0l commented Feb 3, 2025

I tried to reduce to the maximum, was late( or very early ^^) , didn't note/remember why I put '__ add __'

__ len __

Erreur lors du streaming HTML : object of type 'JSXElement' has no len()

__ bytes __

Erreur lors du streaming HTML : %b requires a bytes-like object, or an object that implements __bytes__, not 'JSXElement'
net::ERR_INVALID_CHUNKED_ENCODING 200 (OK)

for radd and encode.

@ethan-0l
Copy link
Author

ethan-0l commented Feb 3, 2025

ok actually didn't reproduce why add and radd, will remove it ..

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.

2 participants