📜 ⬆️ ⬇️

Nagare - a new approach to building web applications

Traveling around the Internet in search of fresh ideas for the next project, I accidentally stumbled upon Nagare - a revolutionary, I think, python-framework, drastically changing the approach to developing web applications. It seems, on Habré there was no review yet, it is time to fix it.


So, what difficulties does a web application developer face in the first place:


As you will see now, using Nagare will solve these problems in very simple and elegant ways. At once I will make a reservation that with this post I am not going to give a short course on Nagare, but just to illustrate an interesting and fresh approach to web programming. Pilots and tank crews should read the documentation , good, there it is clear and without verbiage everything is written.
')
In functional programming, there is such a thing as continuations - continuations. In short, functional languages ​​allow the programmer to get an instant snapshot of the program at any time and place it in a variable. Then this snapshot can be “called” and the program will continue from the same place where it was taken (with the only exception - the return value of the function that made the snapshot will be different). This trick allows you to build very puzzling algorithms, striking in their beauty, after a couple of hours / days / years have been spent on their detailed analysis. But we are not talking about that now. The same trick allowed the developers of Nagare to deviate from the "sessionality", which is property of web applications. An application built on the basis of Nagare is practically no different from the usual one — interruptions related to the loading of pages by the user are, as it were, transparently “disguised” by the framework working through the “continue”. You do not have to worry about saving the data, just assign the values ​​to a variable (or an object field) and it will remain as long as the user session exists. Itself. For example, after the user has passed authorization, you simply write:

   AuthOk = True


and then check:

   if AuthOK:
     .......


Profit? Profit! But that is not all. Why does a developer need to know and remember any URLs? It is necessary to give the user a link with the text, and when he clicks on it - display the desired text. In Nagare, it is done like this:

 h << ha ("This is a link"). action (my_link_action)


This line will generate a link with some (well, what difference does it make to us, with which one?) URL, on clicking on which the my_link_action () procedure will be called, defined somewhere earlier. In the procedure, you can change any variables based on which the page will be generated. If you need to create an input form, then Nagare also allows you to do it without any special steps:

 with h.form (). action (f_act1):
   h << h.input (type = "text", value = ""). action (set_val1)
   h << h.input (type = "text", value = ""). action (set_val2)
   h << h.input (type = "submit", value = "Send!"). action (s_act1)


The user will receive a form with two fields (well, what's the difference, what are their name attributes there?) And the submit-button. After filling (typing in the fields “blablabla” and “hehehe”) and pressing the button, set_val1 (“blablabla”), set_val2 (“hehehe”), f_act1 () and s_act1 () will be called. Everything. The data you have entered, do with them what you want. You can even simplify the task - there is no need to do a separate function for each input. The programmer is lazy and he has that right - his working time is expensive. Let Nagare put the field values ​​straight into the variables! This is done like this:

 v1 = var.Var ("")
 v2 = var.Var ("")

 with h.form (). action (f_act1):
   h << h.input (type = "text", value = ""). action (lambda v: v1 (v))
   h << h.input (type = "text", value = ""). action (lambda v: v2 (v))
   h << h.input (type = "submit", value = "Send!"). action (s_act1)


Voila - v1 () will return “blablabla” to us, and v2 () - “hehehe”!

By the way, did you notice that we somehow did not write any HTML? And why should we? Let the HTML designer write, he gets paid for it. Yes, and tags to close more. Nagare saves the programmer from this tedious duty. For example, create a table:

 with h.table:
   with h.tr:
     with h.td:
       h << "111"
     with h.td:
       h << "222"
   with h.tr:
     with h.td:
       h << "333"
     with h.td:
       h << "444"


Handsomely? Will you still run into a python with its indentation of blocks? That's it!

So what else do we have left? Ajax? Can and Ajax. By the way, why does a programmer know about some Ajax there? The logic of the program does not change, Ajax is there or not Ajax. The same fields, links, pages, blocks, content, etc. They are simply updated not in whole but in parts. What is the business of a programmer before it is updated there? Translation of the project on Ajax in Nagare is done like this:
in one place of the program

 xhtml.Renderer (h)

replace with
 xhtml.AsyncRendrer (h)


... and we immediately have Ajax everywhere! Who does not like, can turn off.
There are still many sorts of chips, such as python-> javascript dynamic translation, templates, component models, databases (via sqlalchrmy / elixir), etc. All this can be found on the site . There are also examples. In general - you are welcome!

PS There are, of course, pitfalls. For example, you need to install stackless python. But believe me - this framework is worth it! In the comments, if anything, you can ask questions. I am not a special guru in Nagare, but I played quite a lot with her.

Source: https://habr.com/ru/post/86595/


All Articles