Repository navigation
Version 0.13 - #704
Version 0.13#704
Conversation
|
Interesting take! I think it gets Starlette closer to a declarative style, which for end users is almost always preferable. So as I understand, the current imperative API ( Just a small note — in most cases Black is probably going to reformat these big lists a bit differently, e.g.: routes = [
Route("/", homepage),
Mount(
"/users",
routes=[
Route("/", users, methods=["GET", "POST"]),
Route("/{username}", user),
],
),
]I think it looks OK, but just wanted to get this out there. When I first saw the Channels documentation with those huge lists made out of strangely-named objects, it didn't look very welcoming to be as a beginner. Luckily, names of Starlette's components are fairly short and explicit, so that shouldn't be a problem here. People can still break things down, e.g. users_routes = [
Route("/", users, methods=["GET", "POST"]),
Route("/{username}", user),
]
routes = [
Route("/", homepage),
Mount("/users", users_routes),
]In larger projects, they'll most likely import these routes from a different module anyway. |
Yes, that's not going away here. However I do think that I'm going to stop documenting it everywhere. Would I like to eventually phase it out? Possibly at some point in the future, maybe. One of the key things I'm trying to emphasize in Starlette is a low-complexity stack, and components such as the
Indeed, yes. I'm actually not super wild about that. The routes = [
Route("/", endpoints.dashboard, name="dashboard", methods=["GET", "POST"]),
Route("/tables/{table_id}", endpoints.table, methods=["GET", "POST"], name="table"),
Route(
"/tables/{table_id}/columns",
endpoints.columns,
methods=["GET", "POST"],
name="columns",
),
Route(
"/tables/{table_id}/delete",
endpoints.delete_table,
methods=["POST"],
name="delete-table",
),
Route(
"/tables/{table_id}/upload", endpoints.upload, methods=["POST"], name="upload"
),
Route(
"/tables/{table_id}/columns/{column_id}/delete",
endpoints.delete_column,
methods=["POST"],
name="delete-column",
),
Route(
"/tables/{table_id}/{row_uuid}",
endpoints.detail,
methods=["GET", "POST"],
name="detail",
),
Route(
"/tables/{table_id}/{row_uuid}/delete",
endpoints.delete_row,
methods=["POST"],
name="delete-row",
),
Route("/500", endpoints.error),
Mount("/static", statics, name="static"),
]I guess we could suggest that folks use # fmt: off
routes = [
Route("/", endpoints.dashboard, name="dashboard", methods=["GET", "POST"]),
Route("/tables/{table_id}", endpoints.table, name="table", methods=["GET", "POST"]),
Route("/tables/{table_id}/columns", endpoints.columns, name="columns", methods=["GET", "POST"]),
Route("/tables/{table_id}/delete", endpoints.delete_table, name="delete-table", methods=["POST"]),
Route("/tables/{table_id}/upload", endpoints.upload, name="upload", methods=["POST"]),
Route("/tables/{table_id}/columns/{column_id}/delete", endpoints.delete_column, name="delete-column", methods=["POST"]),
Route("/tables/{table_id}/{row_uuid}", endpoints.detail, name="detail", methods=["GET", "POST"]),
Route("/tables/{table_id}/{row_uuid}/delete", endpoints.delete_row, name="delete-row", methods=["POST"]),
Route("/500", endpoints.error),
Mount("/static", statics, name="static"),
]
# fmt: onThat's still a bit heavy, so I'm open to conversations there. I do think this is a bit of an awkward point because I think the style expresses the internals as clearly as possible, but it's not neccessarily how I'd want it to display. Also worth noting that it's much nicer for allowing us to do expressive, complex stuff such as... routes = [
Host("api.example.com", name="api", routes=[
Route("/users/", user_list, name="user-list"),
Route("/users/{username}", user_detail, name="user-detail"),
]),
Host("www.example.com", routes=[
Route("/", homepage, name="homepage"),
Mount("/auth", routes=auth.routes, name="auth"),
Mount("/statics", app=statics, name="static"),
])
] |
|
Yes, the big list style has value for sure. When I think about it, in the world of frontend frameworks most routing is defined this way, declaratively, using either an array or an object (at least, Angular and Vue Router work like this). We haven’t been particularly used to it in the Python backend side, apart from a lighter form of it in Django, but I really like the simplicity behind it. Also, I didn’t know Starlette supported subdomain routing via Host! That’s a really handy feature. |
Indeed - it's not actually documented yet (and I need to add support for In practice it's actually a bit awkward to use in local development, so may need to put up a bit of documentation on how to use tools such as ngrok, or how to edit |
|
the routing change makes me ask - do you have any plans for a starlette project generator ? like a "rails new" command. the routing config could be put in a separate file (pretty much like routes.rb) and turn off formatting for black as well. As more and more declarative features get included - a project generator would be very very useful. |
|
@sandys I'm not exactly sure what we'll want to do there. It's possibly that we want to leave that kind of thing for higher level frameworks like Fast API to explore. Or, it might be that we just recommend something along these lines... $ git clone https://github.com/encode/starlette-example myproject
$ cd myproject
$ rm -r .git(Note that the starlette-example repo needs updating in line with the 0.13 changes) Or it could be that, yes, it makes sense for use to have a lightweight |
|
ideally a the alternative is the example project. Actually we are treating hostedapi as pretty much that ! |
|
From past experience, project code generation is not necessarily the best approach when it comes to hinting at best practices. Sure, it might be convenient, but it can turn out to be hard to maintain, and IMO it prevents users from actually thinking about how they'd like to set things up. It might make sense for super-opinionated frameworks like Django, but I don't think it'd bring much value to a super lightweight one like Starlette. I think ideally the documentation should serve as an example, and point to example projects ( Edit: perhaps let's open an issue for this particular topic if there's more discussion needed? |
|
i dont agree with the forced usage of "users should think" and "lightweight frameworks should not have generators" I think a framework can be lightweight and yet handholds users. A great example of this is alembic. Obviously its @tomchristie 's prerogative here ...but there are sufficient examples of fantastic frameworks that do this. |
|
I think it might have sense what @sandys proposes. The beauty of starlette that it fits many purposes as it's also a perfect level for big or custom projects, it's not that high level to customize existing functionality, so you can have full control of what you do and it's not too low level to have unreasonable boilerplate. In case the goal is simple and a lightweight app it might end up as everything in one file and in a few, but you generally wouldn't be thinking about the project structure and its template. Where in case of a bigger project it might be handy to have some guide around (like Django) does. It's also the case where the route decorators become a mess as spread through the whole project. But be that as it may, It's tricky indeed to come up with such a template that would fit custom purpose projects and higher-level frameworks as well. |
+1 on this, I not only fully agree but this is proved on a daily basis, just take a look at issues on https://github.com/tiangolo/full-stack-fastapi-postgresql/issues Most questions have nothing to do with the framework: how I add a celery tasks, my traefik container doesn't work, how do I connect to pgadmin, how can I add a user to the db, how can I debug in docker ? The result is simple, issues are not addressed. |
|
And i genuinely dont see how one can solve it by asking users to RTFM or
think.
This is a ux (user experience) part of a project - there is value in
reducing barrier to adoption.
React does it, rails does it, alembic does it, django does it...as that
tool becomes better, the number of repetitive questions go down.
…On Wed, 6 Nov, 2019, 18:48 euri10, ***@***.***> wrote:
From past experience, project code generation is not necessarily the best
approach when it comes to hinting at best practices.
Sure, it might be *convenient*, but it can turn out to be hard to
maintain, and IMO it prevents users from actually *thinking* about how
they'd like to set things up. It might make sense for super-opinionated
frameworks like Django, but I don't think it'd bring much value to a super
lightweight one like Starlette.
+1 on this, I not only fully agree but this is proved on a daily basis,
just take a look at issues on
https://github.com/tiangolo/full-stack-fastapi-postgresql/issues
Most questions have nothing to do with the framework: how I add a celery
tasks, my traefik container doesn't work, how do I connect to pgadmin, how
can I add a user to the db, how can I debug in docker ?
The result is simple, issues are not addressed.
It effectively makes people lazy and fail to search and think.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#704>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAASYU5XX4TCUCDF252MHYTQSK733ANCNFSM4JJEDCQA>
.
|
|
Let's move any convo on |
|
About the I see how this helps to structure parts by their type, all routing is done in a single place, in a list, and the functions that handle each route are separated in another place. On the other side, my argument in pro of decorator style is that it ties things together by the feature or subject they are related to. So, the code that handles a username is near the declaration of the route I find that especially useful with routes with path parameters like: @app.route('/user/{username}')
def user(request):
username = request.path_params['username']
return PlainTextResponse('Hello, %s!' % username)In this case, the If someone was refactoring that to make it Having those two related texts naming something in different files could, in some cases, make that refactor a bit more difficult. This is similar to the philosophy in React, of having code of different types (HTML-ish JSX and JavaScript) close together, in the same file/component, but related in their subject, not the type of code. And React-router works in a similar way. I actually didn't like React until recently, that I started using it and it made sense to me (I used only Angular and Vue before that). But it also seems that that idea (React itself) ended up being quite popular. The preference between these two can quickly become quite subjective and dependent on personal/team taste, so I understand if this is the preferred way for Starlette. I still wanted to share that additional aspect of it, that might be important. For FastAPI it's quite more relevant, as there's even more "shared information" between the part that declares a route path (the decorator) and the code in the function that handles it, so I'll keep that as the default there. As long as I can keep using decorators in FastAPI, I'm OK with the decision. It currently uses But even if not, if there's a way to update the routes incrementally (I guess some |
|
@tiangolo Yeah, in addition to the obvious awkwardness posed by path parameters, I think the various If those arguments had to be placed in a routes list rather than next to the endpoint definition, I think it would get hard to maintain. I have some ideas about how FastAPI might be changed to make it easier to follow this approach, but I'll discuss those in fastapi/fastapi#687. |
|
Yep, that's what I meant @dmontagu. And, yeah, my plan in FastAPI is to keep decorators. Still, it shouldn't be a problem, FastAPI doesn't have to use the same style as Starlette, it can use it internally as an ASGI toolkit (one of Starlette's its objectives). As long as Starlette supports it. Also, Starlette's goals are probably more inclined to be a very lightweight, simple, ASGI toolkit, with a small surface and a clean and simple codebase (it's beautiful inside) that can be easily extended and used by many other tools. While FastAPI's goals don't include being a toolkit for other frameworks, nor having an as-simple-as-possible codebase. The goals are heavily inclined towards the final API developer's "user/development experience". As an example, all the app HTTP methods like So, it's OK if both projects adopt/suggest different styles in some places, their objectives are probably a bit different. And also, although decorator vs list styles look quite different, supporting decorator style in FastAPI wouldn't be that difficult while still using Starlette's awesomeness. Anyway, we can continue the FastAPI specific discussion in that issue as @dmontagu suggests, but I still wanted to bring some attention to that point of the closeness in code of related subjects, in case that's something important for Starlette itself. |
|
@tiangolo - Very much agree with all the above, yup! 😃 And yeah, totally dig the "closeness of related concerns". I think the overall trade-off is just a bit different for Starlette, than it is for FastAPI. |
* commit 'b8bd1696492e501bd617bd151278999c68b30e2b': (39 commits) Update database.md Update routing.md Drop double square brackets tests in install script (Kludex#718) Show nicer warning when `ujson` isn't installed (Kludex#728) Tweak app signatures (Kludex#723) Added support for publishing wheel files. Fixes Kludex#668 (Kludex#719) 📝 Fixes example typo in README (Kludex#717) Version 0.13.0 Version 0.13 (Kludex#704) List Scout APM as a third party package (Kludex#710) Add Authlib into third party packages. (Kludex#712) Fix a small typo in CORS middleware documentation (Kludex#708) Handle trailing slash redirects properly. (Kludex#707) Strip whitespace on response headers (Kludex#706) Update release-notes.md Remove Bocadillo from third party packages (Kludex#684) Version 0.12.13 Support app configuration on initialization. (Kludex#702) Version 0.12.12 Fix url_for with double mounts (Kludex#701) ...
|
Coming from Flask instead of Django I really appreciate how you can just open up a I was just about to switch to FastAPI, and with it this project. It just suits some projects much better, where getting something up fast (but without being sloppy). That's something I experienced with flask very well: First you have an idea. A simple web app or API which should deliver things. And that's a very pythonic way of doing things. class Main {
public static void main(String args[]) {
// your code here
}
}just to get anything running. So back to our example. The app we're building is now getting more complex and so does our code. So that's growing very good with our app, and if it is getting big there is no problem there. Some apps however never need to grow, they stay simple. That's when setting up a dedicated routing file is more of a burden than a feature. That's why I love the decorator way, it's simple and I have to edit only one file. |
|
I see why forcing the However I'd love to be able to use the old style as I'm now starting with a new project and looking for a good async alternative of I can understand the idea of dropping it from the framework for speed gain and less code, which is a valid goal. Maybe that future plan could instead of dropping it completely just refactor them in a separate project build on Starlette (maybe |
|
Also could you maybe serve a pre-0.13 version of the docs, to make it easier for those who still like to use (or have to use) the decorators? |
Documentation still in progress.
This PR does not significantly remove any public API, but it does start to move us to a new prefered style of routing, moving away from the decorator style.
I think using plain route lists is more clearly decoupled, and gives a clearer indication of what's going on under the hood.
For example...
Similarly, middleware now has a prefered configure-on-init style...
Still to do here...
Review. - We should defer this to a future release.exception_handlers/server_errorstarlette-example.We may also want to reivew our
url_forandurl_path_fordocumentation and API as part of this release version. I'm somewhat keen for us to switch things around there, so that path-only URLs are the default style, and there's less surface area for folks to need to care about configuring Uvicorn's--allow-forward-ips. For example if we were using this:app.url_for(),request.url_for(),url_for()in templates. Return a relative path-only URL.request.absolute_url_for(),absolute_url_for()in templates. Return a fully qualified URL.I'm also pretty keen to switch things up here so that when
debug=Truewe...url_for(...)fails.Something else that I think is interesting here is that we're more clearly exposing that alternative subclasses of
BaseRoutecan also be added to the routing table. For example, it'd be interesting to consider if there's any worthwhile documentation work to do in the ecosystem around eg. noting how you can use FastAPI'sAPIRouteinside a regular Starlette application, to point at a handler which gets their dependency injection treatments, alongside other regularRoutecases, which just get the standardrequestargument.That line of thought is as much about demystifying how the different bits fit together, about making sure that we can display useful routing debug info for higher level frameworks as well as the plain starlette case, and also about enabling other framework authors to take the happy path when building on top of Starlette. (We kinda missed a trick with
Responderhere, which functions differently enough that it's working against the grain.)