-
Notifications
You must be signed in to change notification settings - Fork 249
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
Better way to organize routes into multiple files #217
Comments
In your from starlette.routing import Route
def get_page():
# do whatever you like
pass
routes = [Route("/page", endpoint=get_page, methods=["GET"])] # add all your routes in this list Then in your from fasthtml.common import *
from routes.page import routes as page_routes
from routes.blog import routes as blog_routes
app_routes = [*app_routes, *blog_routes] # add all your routes from other files
app, rt = fast_app(
routes=app_routes,
) I hope it helps :-) |
Yes that is very helpful, thank you! |
@psabhay I actually had to make an additional change to get this to work. The When you call:
This passes a list of normal Starlette
So what we really need to do is create lists of
In main.py:
If you don't do these steps you get a bunch of errors: |
This is how I do it: https://github.com/AnswerDotAI/fh-about/blob/main/main.py |
This is how I do it: from fasthtml.common import *
app, rt = fast_app()
from fasthtml.common import *
from make_app import app
@app.get("/foo", name="foo")
def foo():
return Div("Hello world!", A("Self link", href=app.url_path_for("foo")))
from fasthtml.common import serve
from page import *
serve() |
@jph00 just curious if you would use that approach if each of your sub-pages had more routes? E.g. if each page had a dozen or more routes, would you still import and define the routes in your main file as you do for the |
tbh originally i did it the same way as @Pjt727, but changed it because i figured it might be easier for people to follow and a bit more familiar. i think either is fine though. |
FastAPI solved this issue in a cleaner way with APIRouter, which lets you create routes independently of the app and add them to the app later. Unfortunately it does not seem very easy to implement because RouterX requires a reference to the app. I would be curious to know why. |
This is what I've done and it works really well.
This way, I can implement all of routes as controllers and just link those controllers in the app.py. You could potentially have the first part of app.py as a Hopefully, this helps |
FYI FastHTML has an APIRouter nowadays too. :) |
haha, I've never been more happy at chucking code I've written |
Hi team, amazing project you've built! I currently have a large project where I don't want to put all my routes into one file because it gets too cumbersome. I prefer to have different python files for each page ala Streamlit. But then I'm left with this odd pattern of adding all the routes manually to the main app object in the python file which starts the uvicorn server. See below:
I will have routes defines in
Page1.py
andPage2.py
but I can't decorate them with@app.route()
in those files because that requires importing theapp
object into them, and havingPage1.py
,Page2.py
, etc. run when the app starts. In order to have them run when the app starts I'd have to import the functions intomain.py
and that is a circular import. I have to do this in main.py:For literally all routes in my app. It's unclear when looking at an individual
Page1.py
what functions are routes vs. normal functions and when looking atmain.py
it's unclear how these routes relate to the whole app.The text was updated successfully, but these errors were encountered: