9 ms·
Molten: A modern Python (3.6+) web framework
- krob 8y agoDoes python 3.6 support type hinting? This reminds me of php 7+ with scalor types, and return types now. Didn't think python supported this.
- jakereps 8y agoYes, Python introduced static type hinting in version 3.5 https://www.python.org/dev/peps/pep-0484/ https://www.python.org/dev/peps/pep-0484/
- ubernostrum 8y agoPython introduced the 'typing' module in the standard library in 3.5, and declared that henceforth the annotation syntax was for type hints. Python introduced the annotation syntax (for everything except variables/attributes) in 3.0. The annotation syntax for variables/attributes was introduced in 3.6.
- dfox 8y agoThe feature itself is not type hinting but simply a way to associate arbitrary Python object to essentially any other slot (variable, argument, return value, whatever...) that can hold reference to Python object. The interpreter is supposed to not care about values of such annotations, which makes it a powerful syntactic construct that can be used and abused for various purposes. My two evening project was PyCLOS, which (ab)uses these annotations to provide CLOS style multimethods in python (it is sadly CPython dependent, because the Python side relies on CPython implementation details and while the C extension is not mandatory it is essentially required to get reasonable performance, with somewhat surprising observation that PyCLOS dispatch is measuringly faster than normal attribute access in most cases)
- perturbation 8y agoKind of reminds me of https://github.com/timothycrosley/hug https://github.com/timothycrosley/hug , especially using type hints for validation of request parameters.
- sephware 8y agoHere's a comparison between Molten and Hug by the author of the former: https://old.reddit.com/r/Python/comments/8ta3ve/molten_a_modern_api_framework/e15ygvr/ https://old.reddit.com/r/Python/comments/8ta3ve/molten_a_mod...
- jrs95 8y agoAlso similar to Tom Christie's APIStar https://github.com/encode/apistar https://github.com/encode/apistar
- Bogdanp 8y agoAPIStar was a major inspiration[1]! One of the reasons I built Molten was because Tom Christie took APIStar in a different[2] direction. [1]: https://moltenframework.com/motivation.html https://moltenframework.com/motivation.html [2]: https://docs.apistar.com/#where-did-the-server-go https://docs.apistar.com/#where-did-the-server-go
- gravypod 8y agoThis looks very cool. Is there a reason the HTTP response codes and many other things are exposed as magic string constants?
- zzzeek 8y agoSo refreshing to see a new Python web framework that isn't trying to hoist more event-based non-blocking IO on us.
- coolsunglasses 8y agofoist
- deleted 8y ago[deleted]
- scribu 8y agoFor those who are looking for web frameworks that use async I/O: https://github.com/huge-success/sanic https://github.com/huge-success/sanic https://gitlab.com/pgjones/quart https://gitlab.com/pgjones/quart
- devbug 8y agoI don't get the hate. I can reason about asyncio code without fuss and the throughput/latency impact is worth it. My only pain point is the derth of libraries. There are few if any palatable libraries that provide the powerful abstractions I'm used too. Things like ORMs.
- rsyring 8y agoGrandparents concern about async is documented here: http://techspot.zzzeek.org/2015/02/15/asynchronous-python-and-databases/ http://techspot.zzzeek.org/2015/02/15/asynchronous-python-an...
- dfee 8y agoI get that SA ORM is unfit to run on the eventloop thread, but I’m increasingly unclear if you hold your position that asyncio is bad solely because it makes SA ORM less competitive - and less of a ecosystem standard. The alternative being just using SA Core and something like aiopg, using asyncpg, or using SA in a threadpool (unfortunately, a technique that’s involved enough that the recipe for using SA isn’t a 5 min “gotcha”, re: flask). I like SA. I’ve used it for years, and you’ve helped me on the forums, so I’m appreciative for the work you’ve done. I just don’t understand why you’ve written off an entire style of programming.
- aportnoy 8y agoHow does this compare with Flask?
- Kpourdeilami 8y agoIt seems to come with ORM and request validation, metrics, etc. built-in. Flask is fast for getting started but you end up rebuilding most of the things this framework does on your own as your application grows. It seems like a good middle ground between Flask (minimal and no batteries included) and Django (bloated but very powerful).
- scrollaway 8y agoI don't see the ORM or metrics in the docs. After a cursory look at it, this seems like an interesting, modern replacement for Flask. Types-first frameworks are interesting, as they're still very rare right now.
- tyingq 8y ago"The molten.contrib package contains various functionality commonly required by APIs in the real world such as configuration files, prometheus metrics, request ids, sessions, SQLAlchemy, templating, websockets and more."
- scrollaway 8y agoAh! Missed that, thank you :)
- chrsstrm 8y agoIt looks like there's no template rendering so it would seem that Molten:Flask :: Restify:Express in that it is mostly for build an API.
- Bogdanp 8y agoAuthor here - while molten is API-first, there is built in support for things like sessions[1] and templates[2] for when you need them. The docs could definitely do a better job of highlighting them, though. [1]: https://moltenframework.com/reference.html#sessions https://moltenframework.com/reference.html#sessions [2]: https://moltenframework.com/reference.html#templates https://moltenframework.com/reference.html#templates
- Waterluvian 8y agoIn Python 2, omitting (object) inheritance in a class definition had an actual effect. Is something different in Python 3 that makes this okay, or are we doing the less okay thing because it's cleaner?
- joshuamorton 8y agoYes, it has no effect in python3. There are no old style classes.
- Waterluvian 8y agoOh that's just wonderful. Thank you.
- meowface 8y agoPython 3 cleaned up a lot of other language warts like that one (though some may disagree as to whether or not they were warts). No more `super(SuperLongClassNameHere, self).method()`, no more having to teach beginners to always use raw_input() instead of input(), `0 > None` now raises an exception rather than evaluating to `True`. I personally wish they would just make `self` implicit, but I understand that that adds a lot of magic in a language that currently doesn't have a lot.
- another-cuppa 8y ago> I personally wish they would just make `self` implicit Hell no. Go use Java if you want that. If you find typing self is making you more work then you're not using classes correctly. Write functions and use classes if you need them.
- meowface 8y agoedit: To be clear, I mean making `self` implicit in the method signature - not implicit in the method body like the way Java does it. It's a very minor and aesthetic/cosmetic thing. No, I don't write classes for everything. I write functions for things that should be functions and classes for things that should be classes. But when I do write classes, it's annoying to remember to write `self` in the method signature, annoying to get exceptions because I forgot to add it, annoying to see it since it's just that tiny bit of additional redundant line noise when I just want to quickly glance at a method signature. And literally no other OO language does this that I'm aware of; even ones that are generally way less dynamic than Python. But as I said, I do understand how it came about and why it is the way it is. But on the other hand, it was the exact same story for `super()`, and they did resolve that with "magic". The "magic" floodgates have also opened up a bit more with string interpolation added [1]. So now I think a more reasonable case can be made for making `self` optional. If they could do it for `super()`, they can do it for `self`. It doesn't annoy me that much, though, and I still plan to use Python for a long time even if it's never changed. [1] https://www.python.org/dev/peps/pep-0498/ https://www.python.org/dev/peps/pep-0498/
- andybak 8y agoPost title needs to have "API" in it.
- jihadjihad 8y agoThis looks pretty clean. I would like to see benchmarks that compare between Flask and especially Falcon, which IMO is the gold standard for fast, minimal API frameworks.
- Bogdanp 8y agoThere are some benchmarks[1] in the molten repo as well as instructions for running them yourself. On my machine it ends up being faster than Flask and nearly as fast as Falcon. [1]: https://github.com/Bogdanp/molten/tree/master/benchmarks https://github.com/Bogdanp/molten/tree/master/benchmarks
- JustSomeNobody 8y agoapp = App( components=[ DBComponent(), TodoManagerComponent(), ], routes=[ Include("/todos", [ Route("/", list_todos), Route("/", create_todo, method="POST"), ]), ], ) Wonder what this would look like for a real app and not a toy.
- dternyak 8y agoHad the same thought, so I built https://github.com/dternyak/molten-boilerplate https://github.com/dternyak/molten-boilerplate Gets you pretty close to what a production app looks like. Feedback welcome!
- dternyak 8y agoBogdan has put together an awesome framework with molten that I think can use a lot more visibility. With his permission, I've posted here to get more eyes on it. I'm excited to see how much attention it's getting. For those who want to get started with molten, I've put together a boilerplate for a more full fledged production application here: https://github.com/dternyak/molten-boilerplate https://github.com/dternyak/molten-boilerplate It demonstrates: - OpenAPI (swagger) - SQLAlchemy - alembic - py.test - invoke (task management / CLI) Feedback is super welcome on the boilerplate, which is partially inspired by https://github.com/Bogdanp/molten_cookiecutter https://github.com/Bogdanp/molten_cookiecutter Bogdan has been incredibly friendly and helpful while I've been learning the ins and outs (and pestering him over email). I'm sure he'll be happy to answer any questions here as they come up. Happy hacking!
- tankerdude 8y agoHow does this compare with Connexion? Uses Swagger/OpenAPI that then uses convention (or fully defined) packages, and methods for REST. Runs on top of Flask, and does a lot of all of the validation all from the yaml file. Plumbing taken care for you, etc. so you can focus on business logic?
- Bogdanp 8y agoI'm not familiar with Connexion (this was my first time hearing about it), but it seems, from reading its README, that the two approaches are polar opposites: in Connexion you write the schema first and that is used to hook up your API to your business logic, whereas in molten you write your API using normal Python code and idioms and an OpenAPI schema is generated from that code. As a user of Molten, you don't need to know anything about OpenAPI to be able to use it.
- deleted 8y ago[deleted]
- tankerdude 8y agoMolten does what I've seen in RoR land do. You decorate and document your schema. It then can spit out an OpenAPI spec, use its schema and tooling around OpenAPI. So it becomes a choice. A decision of writing mostly OpenAPI, or writing Python via an API that is defined by someone (or a small set of people). I'd personally rather learn OpenAPI specs as you're going to live in that world anyways, as it gives you so much tooling. It's not hard, and you could take that yaml file and generate code for other languages in case Python's performance specifically for that API (especially it if is a microservice), runs too slowly in the real world and need a faster runtime, etc.
- no_wizard 8y agoIf the author or someone cares to share I’m curious about the decision not to build this on asyncio
- Bogdanp 8y agoHere are my reasons: * I am not a fan of asyncio's API (`ensure_future`), docs (though I understand these two are improving in 3.8) and of the fact that it is incompatible[1] with much of the existing ecosystem of libraries (eg. the SQLAlchemy ORM or any existing libraries that talk to... anything over the network (postgres, redis, memcached, etc.) which need to be written from the ground up in asyncio style or rewritten so their protocol parsing logic is completely separate from the IO that they do (not a bad thing, mind you, but taking battle-tested libraries and rewriting them from the ground up is not the greatest idea)), tools and even builtins (queue.Queue vs asyncio.Queue). * I don't buy into the claimed benefits of explicitly marking yield points: any sufficiently complex code will have enough yield points (`async with`, `async for`, `await`) that reasoning about concurrency in the program isn't going to be any easier than if it had implicit yield points. * I think ergonomics matter for developer happiness and programming with asyncio is not ergonomic in the slightest. * File IO is blocking. Put all of those together and you end up with a system where you can't use any of the popular, battle-tested, libraries out there, where your code is more verbose for minimal gain and where seemingly benign things (reading a file, using a `for` instead of an `async for`) may greatly impact the performance of your program all for a small increase in throughput (assuming it does increase!). [1]: http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ http://journal.stuffwithstuff.com/2015/02/01/what-color-is-y...
- thomasahle 8y ago1) This is fair. 2) Explicit yield points certainly save you from most of the classic threading errors, and having to use special maps and datastructures everywhere. 3) Other than tying into (1) I guess this is a matter of taste. 4) Supposedly this refers to the `open` function? This can be solved using the `aiofiles` module, but again I suppose this ties into (1).
- wodenokoto 8y agoIs this competing with flask or with Django?
- Chirag 8y agoI would say Flask.
- hguhghuff 8y agoWhat’s the benefit / key differences compared to falcon? https://falconframework.org https://falconframework.org
- oblio 8y agoWell, the front page illustrates them quite well, I think.
- hguhghuff 8y agoErrr, I’m looking for some words to answer the question, not to try to infer at an expert level by reading a large amount of detailed technical information, and I didn’t see a comparison with falcon, so no, it does not.
- oblio 8y agoThe Molten front page can be skimmed in 5 minutes or less, it has a lot of empty spaces... Here: Molten supports Python 3.6+. Falcon supports Python 2.6+. Molten uses MyPy for validation. As far as I can see, Falcon does not.
- Bogdanp 8y agoI've used Falcon extensively for about 3 years while at LeadPages. I would say the biggest difference between the two is Molten gives you more out of the box: input validation, doc/schema generation, ORM support, support for multipart/form-data requests (Falcon doesn't actually handle these out of the box!), etc.
- hguhghuff 8y agoSo molten is batteries included essentially, whereas falcon is not. Like flask versus Django. Straight question, wouldn’t it have been easier to fork falcon and add the batteries?
- 8y ago
- drcongo 8y agoI recently started my first Falcon project, but this looks closer to what I was actually hoping Falcon would be. Definitely going to give this a spin, thanks.
- drcongo 8y agoOK, I've been playing with this for an hour or so now and it's magnificent. Exactly what I needed, thank you.
- voycey 8y agoBitdefender sees this site as dangerous? Admittedly it is quite overzealous but something you should look into!