Fuyukai / Kyoukai

[OLD] A fully async web framework for Python3.5+ using asyncio
https://mirai.veriny.tf
MIT License
298 stars 14 forks source link

Additions to the tutorial #27

Closed smlbiobot closed 7 years ago

smlbiobot commented 7 years ago

First of all, thanks for creating this framework. I just downloaded it and am testing it out. Currently going through your docs here: https://mirai.veriny.tf/en/latest/core/gettingstarted.html

And while I thought that it’s nice that you be as brief as possible, may I suggest that you add the source for the entire file somewhere? I had to look up other people’s code using library to know that I have to add:

from kyoukai.util import as_html

app = Kyoukai("my_app_name")

in order to get the basics running. I know that it may seem obvious, but it is unclear from the doc itself that these are needed.

Fuyukai commented 7 years ago

Oh, whoops, I rewrote part of the file and completely forgot to re-add those items.

Give me a minute and I'll update the docs.

smlbiobot commented 7 years ago

Wow thanks for the speedy reply! This is unexpected :)

If in the future you have time, I’d love some help on using jinja2 with it also but not a big issue as I know that is slightly off-topic for this library.

Fuyukai commented 7 years ago

The docs will rebuild automatically and be live in about 10 minutes.

I can help you here, I guess, what's the issue?

smlbiobot commented 7 years ago

No issue — mostly just want to see an example of it somewhere. Or you can perhaps link me to a public repo which uses both Kyuokai + jinja2 to see what’s the ideal way of setting both up?

Fuyukai commented 7 years ago

As far as I know, there's no public repository.

However, the Asphalt usage tutorial + https://asphalt-templating.readthedocs.io/en/latest/ should be able to guide you.

smlbiobot commented 7 years ago

Ah—very sweet! Thanks for the link!

AlbertoOS commented 7 years ago

Just being a bit intrusive here, but what I did to set it up was the following:

from asphalt.core import ContainerComponent, Context
from asphalt.templating.component import TemplatingComponent
from kyoukai.asphalt import KyoukaiComponent
from jinja2 import FileSystemLoader
from pathlib import Path
import kyoukai

app = kyoukai.Kyoukai('app')

class ApplicationComponent(ContainerComponent):
    async def start(self, ctx: Context = None):
        if not ctx:
            ctx = Context()
        self.add_component('templating', TemplatingComponent, backend='jinja2', searchpath=str(Path(__file__).parents[1]) + '/templates', loader_class=FileSystemLoader)
        self.add_component('kyoukai', KyoukaiComponent, ip=SERVER_IP, port=SERVER_PORT, app=app)
        await super().start(ctx)

endpoint = ApplicationComponent()

But it was returning an html as plain text instead of rendering it (jinja2 had processed it though), so where I was returning the html I used

return kyoukai.util.as_html(ctx.jinja2.render('anypage.html', var=var))

which basically just adds "Content-Type": "text/html" to the response headers.

This is what worked for me.