Server-side rendering of React components with data from your Python system
from react.render import render_component
rendered = render_component(
'/path/to/component.jsx',
{
'foo': 'bar',
'woz': [1,2,3],
}
)
print(rendered)For client-side side integrations, refer to the docs.
- Installation
- Basic usage
- Using React on the front-end
- render_component
- Render server
- Django integration
- Settings
- Running the tests
pip install reactpython-react provides an interface to a render server which is capable of rendering React components.
Render requests should provide a path to a JS file that exports a React component. If you want to pass
data to the component, you can optionally provide a second argument that will be used as the component's
props property.
from react.render import render_component
rendered = render_component('path/to/component.jsx', {'foo': 'bar'})
print(rendered)The object returned has two properties:
markup- the rendered markupprops- the JSON-serialized props
If the object is coerced to a string, it will emit the value of the markup attribute.
Render servers are typically Node.js processes which sit alongside the python process and respond to network requests.
To add a render server to your project, you can refer to the basic rendering example for a simple server that will cover most cases. The key files for the render server are:
- server.js the server's source code
- package.json the server's dependencies, installable with npm
There are plenty of solutions for integrating React into the frontend of a Python system, each with upsides and downsides.
- Webpack is currently the recommended build tool for frontend projects. It can compile your files into browser-executable code and provides a variety of tools and processes which can simplify complicated workflows.
- Browserify is another popular tool, which has a lot of cross-over with webpack. It is argurably the easiest of the two to use, but it tends to lag behind webpack in certain functionalities.
For React projects, you'll find that webpack is the usual recommendation - hot module replacement, code-splitting, and a wealth of loaders are the features typically cited. react-hot-loader is a particularly useful tool as it allows changes to your components to be streamed live into your browser.
There are two solutions currently available to integrate webpack into a python system:
- django-webpack-loader integrates webpack's output into your project by leveraging a webpack plugin that generates a file for your python process to consume.
- python-webpack integrates webpack's output by talking to a build server that wraps around webpack.
Both projects can perform the same task of integrating webpack's output. django-webpack-loader is simpler to reason about, it aims to do one thing and do it well. python-webpack's more complex, but offers more features.
Note: older versions of this library used to provide tools for integrating React into your frontend. While those tools tended to provide some conveniences, they also overly complicated deployments, limited the functionalities that you could apply, and locked you in to a limited workflow which was contrary to React's best practices. If you want to persist with the worflow previously offered, the self-mounting components example illustrates the functionality.
Renders a component to its initial HTML. You can use this method to generate HTML on the server and send the markup down on the initial request for faster page loads and to allow search engines to crawl your pages for SEO purposes.
from react.render import render_component
render_component(
# A path to a file which exports your React component
path='...',
# An optional dictionary of data that will be passed to the renderer
# and can be reused on the client-side.
data={
'foo': 'bar'
},
# An optional boolean indicating that React's `renderToStaticMarkup` method
# should be used, rather than `renderToString`
to_static_markup=False,
# An optional object which will be used instead of the default renderer
renderer=None,
)If you are using python-react in a Django project, relative paths to components will be resolved via Django's static file finders.
By default, render_component relies on access to a render server that exposes an endpoint compatible
with react-render's API. If you want to use a different
renderer, pass in an object as the renderer arg. The object should expose a render method which
accepts the path, data, and to_static_markup arguments.
Earlier versions of this library used to run the render server as a subprocess, this tended to make development easier, but also introduced instabilities and inexplicable behaviour. To avoid these issues python-react now relies on externally managed process.
If you only want to run the render server in particular environments, change the RENDER setting to
False. When RENDER is False, the render server is not used, but the similar objects are returned
with the markup attribute as an empty string.
In development environments, it's often easiest to set RENDER to False. This ensures that the render
server will not be used, hence you only need to manage your python process.
Be aware that the render servers provided in the example and elsewhere rely on Node.js's module system which - similarly to Python - caches all modules as soon as they are imported. If you use the render server in a development environment, your code is cached and your changes will not effect the rendered markup until you reset the render server.
In production environments, you should ensure that RENDER is set to True.
You will want to run the render server under whatever supervisor process suits your need. Depending on
your setup, you may need to change the RENDER_URL setting to reflect your setup.
Requests are sent to the render server as POST requests.
The render server connector that ships with python-react adds a ?hash=<SHA1> parameter to the url. The
hash parameter is generated from the serialized data that is sent in the request's body and is intended
for consumption by caching layers.
Depending on your load, you may want to put a reverse proxy in front of the render server. Be aware that many reverse proxies are configured by default to not cache POST requests.
If you want to override the default renderer, one approach is to create a wrapper function so that
you can consistently define the renderer argument to render_component. For example:
from react.render import render_component
class MyRenderer(object):
def render(self, path, props=None, to_static_markup=False):
# ...
def my_render_function(*args, **kwargs):
kwargs['renderer'] = MyRenderer()
return render_component(*args, **kwargs)If you are using python-react in a non-django project, settings can be defined by calling
react.conf.settings.configure with keyword arguments matching the setting that you want to define.
For example:
from react.conf import settings
DEBUG = True
settings.configure(
RENDER=not DEBUG,
RENDER_URL='http://127.0.0.1:9009/render',
)If you are using python-react in a Django project, add 'react' to your INSTALLED_APPS and define
settings in a REACT dictionary.
INSTALLED_APPS = (
# ...
'react',
)
REACT = {
'RENDER': not DEBUG,
'RENDER_URL': 'http://127.0.0.1:8001/render',
}A flag denoting that the render server should be used. If set to False, the renderer will return
objects with an empty string as the markup attribute.
Pre-rendering your components is only intended for environments where serving markup quickly is a must.
In a live development environment, running multiple processes overly complicates your setup and can lead
to inexplicable behaviour due to the render server's file caches. Setting this to False will remove the
need to run a render server next to your python server.
Default: True
A complete url to an endpoint which accepts POST requests conforming to react-render's API.
Default: 'http://127.0.0.1:9009/render'
pip install -r requirements.txt
npm install
python runtests.py