Release 0.1.0 Esteban Castro Borsani
python-react-v8 Documentation
Release 0.1.0 Esteban Castro Borsani
August 19, 2016
Contents
1 User's Guide
1
1.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 API Reference
5
2.1 API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 Additional Notes
7
3.1 Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Python Module Index
9
i
ii
CHAPTER 1
User's Guide
1.1 Installation
1.1.1 Compatibility
What v8-cffi supports
1.1.2 Pip
Latest version can be installed through pip: $ pip install python-react-v8
1.2 Usage
1.2.1 Quick-start
import react react.set_up() react.utils.load_libs(['./bundle.js']) react_ = react.React({
'component': 'Counter', 'data': {'InitialCount': 10}}) react_.render() # Count: 10 First, we must call set_up(), this will initialize the V8 machinery. It must be call once in the program lifetime so it's a good idea to call it at import time or before starting the web-server. Then, we load the js bundle, it should be the same that is provided to the browser. Then, we create the React object, passing the parameters the underlying JS renderer function will receive. It must contain serializable (as json) keys and variables. Finally, we call the render() method, to run the JS renderer function and get its return value, which usually is a serializable object containing the rendered component and other extra data like an error message, redirection, etc.
1
python-react-v8 Documentation, Release 0.1.0
1.2.2 Fake web-framework
import react
from my_web_framework import render, orm, runserver
def index(request): """A naive implementation for a fake web-framework""" data = orm.query().only('name', 'age', 'location') react_ = react.React({ 'url': request.get_full_url(), 'data': data}) context = { 'content': react_.render(), 'data': react_.to_json(data)}
return render('index.html', context)
if __name__ == '__main__': react.set_up() # Initialize V8 machinery react.utils.load_libs(['./bundle.js']) runserver(index)
This is pretty much the same as the "quick start" example.
But there are two things to have in mind:
1. Always make the query including the fields that the react view requires, don't fetch everything, doing so may end up leaking information since it gets exposed as a JS object within the HTML.
2. The data passed to react should be exactly the same as the data provided by the API endpoint for that view, so the query should be the same.
1.2.3 Multiple stores
# ... data = {
'postIts': orm.query().all().only('id', 'text'), 'comments': orm.query().all().only('id', 'created_by', 'comment_html') } # ...
Seriously.
1.2.4 Handling errors
import react
# ... try:
content = react_.render() except react.excepts.V8Error as err:
logger.error(err) content = '' # ...
2
Chapter 1. User's Guide
python-react-v8 Documentation, Release 0.1.0
When handling exceptions, there is not much to do other than set an empty content and let react render it client-side. The data can still be pre-loaded, assuming the error wasn't thrown by the load function. It may be a good idea to log the data (as json) to replicate the error later. Since most of the logic is the same client-side, once you have replicated it, it can be debugged in the web-browser.
1.3 Recipes
1.3.1 Simple React
from react import React
from my_web_framework import excepts
class SimpleReact(React):
@property def data(self):
return self.to_json(self.opts['data'])
def render(self): res = self.to_dict( super().render()) status = res['status']
if status == 500: raise excepts.HTTPError(res['error'])
if status == 302: raise excepts.HTTPRedirect(res['redirection'])
if status == 404: raise excepts.HTTPNotFound
return res['result']
1.4 Limitations
1.4.1 Context
There is just one context. There is no sandboxing. This means all requests to the server will share the same JS context. Creating a new JS context and loading the libs into it for every request is quite slow. A solution is to provide a way to create or reset the stores before doing anything. Node.js suffers from the same issues.
1.4.2 Event Loop
There is no event loop. This means it is not possible to run asynchronous operations.
1.3. Recipes
3
python-react-v8 Documentation, Release 0.1.0
Most (all?) async functions are not available. For example, calling setTimeout will throw a ReferenceError. This is usually not a problem since it's something you don't want to do. Rendering react components to string (serverside) is synchronous. Any async operation should take place before calling react, therefore, it can be done in python code. Here is a react_issue discussing this.
4
Chapter 1. User's Guide
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
Related searches
- minecraft 0 1 0 download
- 1 or 2 374 374 1 0 0 0 1 168 1 1 default username and password
- 1 or 3 374 374 1 0 0 0 1 168 1 1 default username and password
- 1 or 2 711 711 1 0 0 0 1 168 1 1 default username and password
- 1 or 3 711 711 1 0 0 0 1 168 1 1 default username and password
- 1 or 2 693 693 1 0 0 0 1 168 1 1 default username and password
- 1 or 3 693 693 1 0 0 0 1 168 1 1 default username and password
- 1 or 2 593 593 1 0 0 0 1 or 2dvchrbu 168 1 1 default username and password
- 1 or 3 593 593 1 0 0 0 1 or 2dvchrbu 168 1 1 default username and password
- 1 or 2 910 910 1 0 0 0 1 168 1 1 default username and password
- 1 or 3 910 910 1 0 0 0 1 168 1 1 default username and password
- 192 1 or 2 33 33 1 0 0 0 1 1 1 default username and password