The routing table's keys aren't strings, they're regular expressions. Indexing into the dict to pick a route wouldn't work like you think it will. For example, consider a request to /items/13 which would be served by the route /items/\d+. Because those strings are not identical, indexing a dict would not raise a KeyError instead of finding the appropriate route.
As opposed to plain strings? Because regular expressions allow binding a single handler to any number of paths. In some cases this might be feasible...
def handle_static(request):
...
# enulating the regex ``static/.*``
import pathlib
static_dir = pathlib.Path(__file__).parent / 'static'
for file in static_dir.iterdir():
app.route(f'static/{file.name}')(handle_static)
In others it's definitely not (try recreating /items/\d+ without registering /items/0, /items/1, /items/2 off to infinity).
Why use regex as opposed to a bespoke router like Flask uses? Because OP limited themselves to one <100-line file, and that's probably not enough to implement a good router on its own, let alone the rest of the web framework. Regexes can be made to work for routing, as evidenced by OP doing exactly that, and they're part of the standard library so no dependencies needed.
How does Flask implement routes with params like /items/<num>? It’s been a minute since I’ve use Flask but I believe that’s how they do it.
The angle brackets on their own should be a sign that this is not the case, because that's not regular expression syntax. Flask uses werkzeug.routing instead. In fact, the module docs specifically call out the difference:
When it comes to combining multiple controller or view functions (however you want to call them) you need a dispatcher. A simple way would be applying regular expression tests on the PATH_INFO and calling registered callback functions that return the value then.
This module implements a much more powerful system than simple regular expression matching because it can also convert values in the URLs and build URLs.
I like the "Key Importance Points of Implementation". I've never seen anything like it before, but I have always wanted to do something similar for smaller projects where I am just exploring some new concepts. Nicely done!
I notice you have an exception with a body which is just pass. In this scenario it's a cool feature of python that a docstring on its own makes the declaration legal and let's you remove the pass, and while you're there it adds a a touch of readability.
The last line of the framework is on 99, then there is another 66 lines part of __main__ that just setup some basic usage examples, which can be removed or ignored.
42
u/Agent281 Jul 03 '22
Awesome project! Clean little projects like this make me appreciate python so much. Thanks for posting this!
BTW, it looks like you left the address hard coded in the run method (line 82) and you aren't using the address parameter. Might want to switch that.
3
u/xxPoLyGLoTxx Jul 03 '22
Seems cool, but I’m a total noob. What would you use this for?
31
u/SillyKnights Jul 03 '22
That’s awesome! I use Django and FastAPI, but even looking at the source code those seem like magic. Your code is incredibly self documenting.
2
u/mahtats Jul 03 '22
Why not make the route table a dict with a list as the value and extend the functions mapped to it?
9
u/justanothersnek Jul 04 '22
More minimal than bottle? Impressive!
4
u/aby-1 Jul 03 '22
This is super cool!! It is great how much you can achieve with good understanding of the matter and a little bit of code.
4
u/YXAndyYX Jul 03 '22
The run method doesn't use the address parameter, it always binds to 0.0.0.0 as far as I can see.
3
u/jarulsamy Jul 03 '22
I like the "Key Importance Points of Implementation". I've never seen anything like it before, but I have always wanted to do something similar for smaller projects where I am just exploring some new concepts. Nicely done!
3
u/MarsupialMole Jul 04 '22
Nice.
I notice you have an exception with a body which is just
pass. In this scenario it's a cool feature of python that a docstring on its own makes the declaration legal and let's you remove thepass, and while you're there it adds a a touch of readability.2
u/c_is_4_cookie Jul 03 '22
Really great project.
2
u/Unlucky-Drawing8417 Jul 03 '22
Nice job
2
u/_ologies Jul 03 '22
This could almost run on MicroPython on an ESP32
2
u/M_dev20 Jul 04 '22
Awesome work man.
Definitely gonna take a look, we always "accept" things on WebFrameworks without worrying about what is happening behind the scenes.
1
u/MasterFarm772 Jul 04 '22
Wow, it will be such a great resource to understand what happens under the hood
1
u/bacondev Py3k Jul 04 '22
Looks like more than one hundred lines? Still impressive though.
1
u/iair1234 Jul 04 '22
What "production-ready" feature is this lacking and how could those be implemented in a production Fork.
-2
u/Environmental-Ad271 Jul 04 '22
Lines of codes. How does it help framework for a user?