Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Hope robyn can support returning f-string format #338

Closed
shaohaiyang opened this issue Dec 15, 2022 · 10 comments
Closed

Comments

@shaohaiyang
Copy link

Current Behavior
Could not support return f-string response

for example:

image

@sansyrox
Copy link
Member

Hey @shaohaiyang . Robyn does support returning f-strings. For example something like this will work

from robyn import Robyn

app = Robyn(__file__)


@app.get('/')
def index():
    var = "world"
    return f"Hello, {var}"


if __name__ == "__main__":
    app.start(port=8000)

However, you are trying to return an HTML. You can either use templates(https://sansyrox.github.io/robyn/#/features?id=templates) or static_file(https://sansyrox.github.io/robyn/#/examples?id=serving-simple-html-files)) to serve the HTML.

@sansyrox
Copy link
Member

I hope that helps. Lmk if you need some other information. Or we can close this issue. 😄

@shaohaiyang
Copy link
Author

Thank you very much for your reply. According to my test, """ is not supported, but it can be supported by newline formatting.

image

So, this issue could be close.

BTW, I'm testing using robyn, htmx in a new project. It is a good idea.

@shaohaiyang
Copy link
Author

@sansyrox ,If it can support f """ format, it will works better with jinja2.

Thank you very much for your reply. According to my test, """ is not supported, but it can be supported by newline formatting.

@shaohaiyang shaohaiyang reopened this Dec 18, 2022
@sansyrox
Copy link
Member

@shaohaiyang , the traditional f-string format should already work. I will add some documentation regarding it today :D

@sansyrox
Copy link
Member

@shaohaiyang ,

can you try if this works for you

from robyn import Robyn, jsonify
from pathlib import Path
import os


app = Robyn(__file__)


@app.get("/")
def template_render():
    context = {"title": "Hello World!", "description": "This is a description"}
    return {
        "status": "200",
        "body": f"""<html>Hellow { context['title'] }</html>""",
        "headers": jsonify({"Content-Type": "text/html"}),
    }


if __name__ == "__main__":
    app.start(port=8000)

@shaohaiyang
Copy link
Author

yes,it can works but also need backslash :(

@sansyrox
Copy link
Member

@shaohaiyang , actually no.

This works now :D

Without backslash! :D

from robyn import Robyn, jsonify
from pathlib import Path
import os


app = Robyn(__file__)


@app.get("/")
def template_render():
    context = {"title": "Hello World!", "description": "This is a description"}
    return {
        "status": "200",
        "body": f"""
        <html>Hellow { context['title'] }
        <p>{ context['description'] }</p>
        </html>""",
        "headers": {"Content-Type": "text/html"},
    }


if __name__ == "__main__":
    app.start(port=8000)

@shaohaiyang
Copy link
Author

Yes, It works very well when i upgrade robyn to latest verson 0.20.0, thanks again very much .

@sansyrox
Copy link
Member

Perfect. I am glad it works! :D

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

No branches or pull requests

2 participants