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

[BUG] Render Template #339

Closed
Paroxyste opened this issue Dec 16, 2022 · 19 comments
Closed

[BUG] Render Template #339

Paroxyste opened this issue Dec 16, 2022 · 19 comments
Labels

Comments

@Paroxyste
Copy link

Hello to all,

I have installed Robyn, via pip under python 3.10.6 on Mac M1.
I wanted to test the render_template but I have a problem. I copied the code from the doc but it doesn't work.

from robyn import Robyn
from robyn.templating import JinjaTemplate
from pathlib import Path
import os

current_file_path = Path(__file__).parent.resolve()
JINJA_TEMPLATE = JinjaTemplate(os.path.join(current_file_path, "templates"))

app = Robyn(__file__)

@app.get("/template_render")
def template_render():
    context = {
        "framework": "Robyn",
        "templating_engine": "Jinja2"
    }

    template = JINJA_TEMPLATE.render_template(template_name="test.html", **context)
    return template

app.start(port=5000)
{# templates/test.html #}

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Results</title>
</head>

<body>
  <h1>{{framework}} 🤝 {{templating_engine}}</h1>
</body>
</html>

Jinja does a good job of replacing the values (framework, templating_engine) but the rendering prints the html code in the browser (i use Brave Browser) ...

image

Do you have any idea where this is coming from?

Thanks

@shaohaiyang
Copy link

You should be insert a default get router before the template render:

app = Robyn(__file__)

@app.get("/")
@app.get("/template_render")
def template_render():

@Paroxyste
Copy link
Author

Thank you for your answer, but it remains unchanged.

from robyn import Robyn
from robyn.templating import JinjaTemplate
from pathlib import Path
import os

current_file_path = Path(__file__).parent.resolve()
JINJA_TEMPLATE = JinjaTemplate(os.path.join(current_file_path, "templates"))

app = Robyn(__file__)

@app.get("/")
@app.get("/template_render")
def template_render():
    context = {
        "framework": "Robyn",
        "templating_engine": "Jinja2"
    }

    template = JINJA_TEMPLATE.render_template(template_name="test.html", **context)
    return template

app.start(port=5000)

Now in 127.0.0.1:5000 i've got :
image

But in 127.0.0.1:5000/render_template i've got white blank page.

@shaohaiyang
Copy link

shaohaiyang commented Dec 17, 2022

It works on my chrome , safari, firefox, even lynx CLI browser when i copy the codes exactly the same. Have you ever tried another browser?

image

@Paroxyste
Copy link
Author

I don't understand, I've the same problem with firefox and safari:

image

What version of python and robyn are you using?

@sansyrox
Copy link
Member

@Paroxyste , can you please tell me how are you running your code and what is your directory structure?

@sansyrox
Copy link
Member

@shaohaiyang , this shouldn't be the case. Does it not work without a default route for you?

You should be insert a default get router before the template render:
app = Robyn(file)
@app.get("/")
@app.get("/template_render")
def template_render():

@sansyrox
Copy link
Member

@Paroxyste , I think I might know your issue. Can you please try running it at a port different from 5000? Maybe 5001?

Lmk if that works and then I might be able to come up with an explanation.

1 similar comment
@sansyrox
Copy link
Member

@Paroxyste , I think I might know your issue. Can you please try running it at a port different from 5000? Maybe 5001?

Lmk if that works and then I might be able to come up with an explanation.

@Paroxyste
Copy link
Author

Paroxyste commented Dec 17, 2022

I run

python main.py    or    python main.py --dev

image

My structure code is :
image

I tried by changing port and html code, but the result is identical

image

@sansyrox
Copy link
Member

@Paroxyste , this is a bit strange as I copied everything from your code and It is working for me.

Which version of Robyn are you using?

@sansyrox
Copy link
Member

sansyrox commented Dec 17, 2022

Can you please share your main.py too ? I want to make sure that we are not having some unintended mistake there

@Paroxyste
Copy link
Author

Paroxyste commented Dec 17, 2022

main.py

# -----------------------------------------------------------------------------
# !/usr/bin/python3
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------


from pathlib          import Path
from robyn            import Robyn
from robyn.templating import JinjaTemplate

import os

# -----------------------------------------------------------------------------
# APP: APP CONFIG
# -----------------------------------------------------------------------------

current_file_path = Path(__file__).parent.resolve()
JINJA_TEMPLATE = JinjaTemplate(os.path.join(current_file_path, "templates"))


app = Robyn(__file__)

@app.get("/")
@app.get("/template_render")
def template_render():
    context = {
        "framework": "Robyn",
        "templating_engine": "Jinja2"
    }

    template = JINJA_TEMPLATE.render_template(template_name="test.html", **context)
    return template

app.start(port=5001)

image

@sansyrox
Copy link
Member

@Paroxyste , can you try running this to see if that works for you

from robyn import Robyn, jsonify
from robyn.templating import JinjaTemplate
from pathlib import Path
import os

current_file_path = Path(__file__).parent.resolve()
JINJA_TEMPLATE = JinjaTemplate(os.path.join(current_file_path, "templates"))

app = Robyn(__file__)

@app.get("/")
def template_render():
    context = {
        "framework": "Robyn",
        "templating_engine": "Jinja2"
    }

    template = JINJA_TEMPLATE.render_template(template_name="test.html", **context)
    return {
        "status": "200",
        "body": template,
        "headers": jsonify({"Content-Type": "text/html"})
    }


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

@Paroxyste
Copy link
Author

Yes that work fine thanks very much

@sansyrox sansyrox reopened this Dec 17, 2022
@sansyrox
Copy link
Member

Reopening this means that we should return this header by default.

 "headers": jsonify({"Content-Type": "text/html"})

I will create a PR for this.

@Paroxyste
Copy link
Author

For the jinja implementation would it be possible to add something to load the resources like Flask:

{{ url_for('static', filename='images/planets.jpeg') }}

or Starlette :

{{ url_for('static', path='images/planets.jpeg') }}

or for a route:

{{ url_for('/') }}

Because currently using url_for makes the template crash and returns a 500 error ...

Out put this point the integration of jinja works well good job ;)

@sansyrox
Copy link
Member

@Paroxyste , sure. I can have a look at this :D

@koryca
Copy link

koryca commented Jun 27, 2023

We are working on this issue but it looks like it's already solved. I tried to produce the issue and my browser shows no problem with or without "headers": jsonify({"Content-Type": "text/html"})
robyn/templating.py
Is this issue still ongoing?

@sansyrox
Copy link
Member

sansyrox commented Dec 2, 2023

This issue was solved but was kept open for Jinja template resource loading. Closing this.

@sansyrox sansyrox closed this as completed Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants