Skip to main content

A short description for your project.

Project description

Django web components
---------------------

Django-brick is a library that implements web components at server-side for
your Django application. The goal is to reuse code by building simple pluggable
pieces. Web components, as it is usually understood, is a client-side technology.
Think of JavaScript libraries such as ReacJS, Polymer, Vue.js, X-tag etc.

...

Enter the brick
---------------

A brick is a Python object/component that has a well defined interface to
present itself to the client. Usually this means that it can render itself as
HTML5 (but sometimes we may need more complicated behaviors). Pehaps the most
simple brick that you can use is just a HTML5 tag. Django-bricks implement these
building blocks in the :mod:`bricks.tags` module. The most important action a
:cls:`bricks.Tag` brick can make is to render itself as HTML:

>>> from bricks import tags
>>> print(tags.p("Hello World!", class_='hello'))
<p class="hello">Hello World!</p>

Python and HTML have very different semantics. Indeed, HTML's syntax around
tag attributes + children nodes does not have a very natural counterpart in most
programming languages. Of course this can be replicated in a imperative style,
but the end result often feels awkward. We introduce a mini-language to declare
HTML fragments in a much more natural way:

>>> from bricks.tags import div, p, h1
>>> doc = \
... div(class_='foo')[
... h1('Hello World'),
... p('This is a simple HTML fragment written in Python!'),
... ]

The most simple interface a Brick object can expose is to render itself as HTML.
All bricks should define a `.render(request)` method that renders the brick as
an HTML string:

>>> bricks.request import FakeRequest
>>> request = FakeRequest()
>>> doc.render(request)
'<div class="foo"><h1>Hello World</h1><p>This is a simple HTML fragment written in Python!</p></div>'

In Django, this is accomplished by a template filter:

.. code-block:: html

<html>
<body>
{{ doc|render }}
</body>
</html>


:cls:`bricks.Tag` objects may use a declarative syntax more similar to HTML


.. comment
Srvice is a library that aims to integrate a Python server with a Javascript
client via remote calls. With Srvice, the client can transparently call
functions defined in server. The server might also respond with instructions
that execute arbitrary Javascript code in the client.

Let us define a function in the client:

.. code-block:: python

from import srvice

@srvice.api
def get_user_email(request, username):
if can_read_email(request.user, username):
return email_from_username(username)
else:
raise PermissionError

# This function must be associated with some url in your application
urlpatterns [
...,
'^get-user-email/$', get_user_email.as_view(),
]


In the client, we call the function defined in the some URL point using the
srvice object:

.. code-block:: javascript

srvice.call('get-user-email', 'paulmcartney').then(function (email) {
var contact = currentContact();
contact.email = email;
})


Communication is done using JSON strings that pass function arguments and
results from client to server and vice-versa.

This is only the very basic that Srvice can do. Please check the documentation
for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-bricks-0.1.0.tar.gz (41.6 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page