<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Alessandro Molina</title>
    <description>The latest articles on Forem by Alessandro Molina (@__amol__).</description>
    <link>https://forem.com/__amol__</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2936%2Fme.jpg</url>
      <title>Forem: Alessandro Molina</title>
      <link>https://forem.com/__amol__</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/__amol__"/>
    <language>en</language>
    <item>
      <title>Going Isomorphic with Python and React</title>
      <dc:creator>Alessandro Molina</dc:creator>
      <pubDate>Sat, 11 Feb 2017 20:54:46 +0000</pubDate>
      <link>https://forem.com/__amol__/going-isomorphic-with-python-and-react</link>
      <guid>https://forem.com/__amol__/going-isomorphic-with-python-and-react</guid>
      <description>&lt;p&gt;Isomorphic web applications are applications whose part or all the code is executed both on server and client. The server and the browser share the part or all the code.&lt;/p&gt;

&lt;p&gt;One of greatest goals that isomorphic solutions can achieve is improving SEO for Single Page Applications, but so far such features were mostly available for JavaScript developers who could run same code on Browser and NodeJS.&lt;/p&gt;

&lt;p&gt;As Browsers only supports JavaScript (unless esoteric technologies are involved) all the widespread libraries and frameworks available to create isomorphic applications expect to run on JavaScript and as a Python developer we were pretty much out of choices.&lt;/p&gt;

&lt;p&gt;Being dissatisfied with this situation is what lead me to work on &lt;a href="https://github.com/amol-/dukpy"&gt;DukPy&lt;/a&gt;, a Python library that aimed at removing the need for NodeJS from my work toolchain (hope it will for other people too).&lt;/p&gt;

&lt;p&gt;One of the most widespread isomorphic web frameworks is &lt;a href="https://facebook.github.io/react/"&gt;React&lt;/a&gt;, which allows to render react components server and attach dynamic behaviours to them once they reach the browser.&lt;/p&gt;

&lt;p&gt;A few months ago I wrote a short article on &lt;a href="https://medium.com/@__amol__/es2015-and-react-in-pure-python-environment-b326dc15012c"&gt;How to use ES2015 and JSX in Python web applications&lt;/a&gt; without the need for NodeJS or WebAssets.&lt;/p&gt;

&lt;p&gt;But it didn't cover how an Isomorphic application could be created, the javascript code of the application was still running on the browser and React Components couldn't provide a pre-rendered version. So I decided to start this article which showcases how to use DukPy and React together to write an isomorphic web application with Python without even installing NodeJS.&lt;/p&gt;

&lt;p&gt;If you have not read it yet, make sure to take a look at the ES2015 article as this one will take for granted the concepts explained there are already known.&lt;/p&gt;




&lt;p&gt;I'll take for granted that you already have all the required Python packages installed as showcased in the &lt;a href="https://medium.com/@__amol__/es2015-and-react-in-pure-python-environment-b326dc15012c"&gt;ES2015 article&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip install TurboGears2
$ pip install Kajiki
$ pip install tgext.webassets
$ pip install dukpy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once all the required pieces are in place we can start by making an application that renders a React Component on client side, then we will make the same component render on server and have the browser take over from there.&lt;/p&gt;

&lt;p&gt;To do so we will create an &lt;strong&gt;statics/js&lt;/strong&gt; directory where we will put all our JavaScript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir statics
$ mkdir statics/js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to download &lt;strong&gt;react&lt;/strong&gt; and &lt;strong&gt;react-dom&lt;/strong&gt; into that directory so that they are available to our web app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd statics/js
$ curl -O 'https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.js'
$ curl -O 'https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react.js'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last but not least we need the Component itself, which will be a simple &lt;em&gt;HelloWorld&lt;/em&gt; component.&lt;br&gt;
Our component will be declared inside a &lt;strong&gt;statics/js/HelloWorld.jsx&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class HelloWorld extends React.Component {
  render() {
    return (
      &amp;lt;div className="helloworld"&amp;gt;
        Hello {this.props.name}
      &amp;lt;/div&amp;gt;
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we will create an &lt;strong&gt;app.py&lt;/strong&gt; file where we will put the Python code that will start the webserver, create the web application, compile JSX to plain js and serve the index page which will render the component itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import tg
from tg import AppConfig
from tg import TGController
from tg import expose
import kajiki

page = kajiki.XMLTemplate(u'''&amp;lt;html&amp;gt;
    &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt;
    &amp;lt;body&amp;gt;
      &amp;lt;div id="isomor"&amp;gt;&amp;lt;/div&amp;gt;

      &amp;lt;script py:for="m in g.webassets['bundle.js'].urls()"
              src="$m"&amp;gt;
      &amp;lt;/script&amp;gt;
      &amp;lt;script&amp;gt;
ReactDOM.render(
    React.createElement(HelloWorld.HelloWorld, { name: "World" }),
    document.getElementById('isomor')
);
      &amp;lt;/script&amp;gt;
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
''', mode='html5')


class RootController(TGController):
    @expose()
    def index(self):
        return page(dict(
            g=tg.app_globals
        )).render()


config = AppConfig(minimal=True, root_controller=RootController())
config.renderers = ['kajiki']
config.serve_static = True
config.paths['static_files'] = 'statics'

from webassets.filter import register_filter
from dukpy.webassets import BabelJSX
register_filter(BabelJSX)

import tgext.webassets as wa
wa.plugme(
    config,
    options={
        'babel_modules_loader': 'umd'
    },
    bundles={
        'bundle.js': wa.Bundle(
            'js/react.js',
            'js/react-dom.js',
            wa.Bundle(
                'js/HelloWorld.jsx',
                filters='babeljsx',
            ),
            output='assets/bundle.js'
        )
    }
)

application = config.make_wsgi_app()

from wsgiref.simple_server import make_server
print("Serving on port 8080...")
httpd = make_server('', 8080, application)
httpd.serve_forever()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you try to run the saved &lt;strong&gt;app.py&lt;/strong&gt; file with such content and all the dependencies are correctly in place you should see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ python app.py
No handlers could be found for logger "tg.configuration.app_config"
Serving on port 8080...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Heading your browser to &lt;a href="http://localhost:8080"&gt;http://localhost:8080&lt;/a&gt; should greet you with an &lt;em&gt;“Hello World”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--my8Qux0j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2ASD4GWZEbEW7gsmejEndYYg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--my8Qux0j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2ASD4GWZEbEW7gsmejEndYYg.png" alt="HelloWorld"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If anything is unclear, make sure you start from the previous &lt;a href="https://medium.com/@__amol__/es2015-and-react-in-pure-python-environment-b326dc15012c"&gt;React in Pure Python&lt;/a&gt; post, as that will explain step by step what happened so far.&lt;/p&gt;




&lt;p&gt;Now that our app is in place we can start introducing server side rendering for React.&lt;/p&gt;

&lt;p&gt;This requires one additional component we will have to download into our &lt;strong&gt;statics/js&lt;/strong&gt; directory, the &lt;strong&gt;react-dom-server&lt;/strong&gt; library which allows server side rendering of React&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd statics/js
$ curl -O 'https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom-server.js'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we are going to render our component into our Python web application, to do so we are going to rely on DukPy for the actual rendering and WebAssets for providing the required dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import json
from dukpy import JSInterpreter, jsx_compile
from markupsafe import Markup


class ReactRenderer(object):
    def __init__(self, jspath):
        self.jspath = jspath
        self.jsi = JSInterpreter()
        self.jsi.loader.register_path(self.jspath)
        self.components = {}
        self.initialized = False

    def _init(self):
        if self.initialized:
            return

        bundle_js = tg.app_globals.webassets['bundle.js']
        self.jsi.evaljs(
            [f.data() for f in bundle_js.build()] +
            ["var ReactDOM = require('react-dom-server');"]
        )
        self.initialized = True

    def render(self, component, **kwargs):
        self._init()
        code = "ReactDOM.renderToString(React.createElement({component}, {args}), null);".format(component=component, args=json.dumps(kwargs))
        return Markup(self.jsi.evaljs(code))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;ReactRenderer&lt;/em&gt; is a convenience class that will create a DukPy interpreter with &lt;strong&gt;React&lt;/strong&gt; and our &lt;strong&gt;HelloWorld&lt;/strong&gt; component preloaded (through the &lt;strong&gt;bundle.js&lt;/strong&gt; WebAssets bundle we already declared) and &lt;strong&gt;react-dom-server&lt;/strong&gt; loaded through &lt;strong&gt;require&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In fact the class consists of a single &lt;strong&gt;render()&lt;/strong&gt; method which will initialise the interpreter (if it's not already initialised) and will then render the specified React Component. So we can use this class to render any Component that was available into our &lt;strong&gt;bundle.js&lt;/strong&gt; including the HelloWorld one.&lt;/p&gt;

&lt;p&gt;Only part left is we need to create it and provide it to our &lt;strong&gt;index()&lt;/strong&gt; action so that it can use it to render the component. For convenience and as usually I'll need the ReactRenderer object available everywhere I'll make it available into the configuration of my application&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
config.react_renderer = ReactRenderer(
   os.path.join(os.path.dirname(__file__), 'statics', 'js')
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure you add this line before creating the TurboGears application (so before &lt;code&gt;make_wsgi_app&lt;/code&gt;). The argument provided to ReactRenderer is the path where it can find any additional javascript module that will be loaded through &lt;strong&gt;require&lt;/strong&gt;, in this case as we downloaded &lt;em&gt;react-dom-server&lt;/em&gt; in &lt;strong&gt;statics/js&lt;/strong&gt; that's the specified path.&lt;br&gt;
Now that our ReactRenderer is in place we can edit our &lt;strong&gt;index&lt;/strong&gt; action and provide the react renderer to our HTML template&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class RootController(TGController):
    @expose()
    def index(self):
        return page(dict(
            render_react=tg.config['react_renderer'].render,
            g=tg.app_globals
        )).render()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you properly added the &lt;code&gt;render_react&lt;/code&gt; value to the ones the controller action provides to the &lt;code&gt;page&lt;/code&gt; template we can now change the template itself to render the component.&lt;/p&gt;

&lt;p&gt;If you remember we previously had an empty &lt;code&gt;isomor&lt;/code&gt; div&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="isomor"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that div acted only as a target for our &lt;code&gt;ReactDOM.render&lt;/code&gt; call which rendered the component and placed it into the div.&lt;/p&gt;

&lt;p&gt;This was pretty clear through the fact that our page when loaded was empty for a moment and then the content appeared a little later when React was able to render it.&lt;/p&gt;

&lt;p&gt;What we are going to do is replacing that empty div with one with the component pre-rendered inside:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="isomor"&amp;gt;${render_react('HelloWorld.HelloWorld', name='World')}&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;render_react&lt;/code&gt; callable is in fact the &lt;code&gt;ReactRender.render&lt;/code&gt; method we provided from the action. If you remember the first argument is the Component that should be rendered (in this case &lt;code&gt;HelloWorld&lt;/code&gt; from the &lt;code&gt;HelloWorld&lt;/code&gt; module) and any additional keyword argument is passed as a property of the component. In this case we are providing the &lt;code&gt;name=World&lt;/code&gt; property (same as we did in the &lt;code&gt;React.createElement&lt;/code&gt; call).&lt;/p&gt;

&lt;p&gt;Note that it is really important that any property provided to the Component when rendering it from python matches those provided to the &lt;em&gt;React.createElement&lt;/em&gt; call in JS or React will complain and will replace the div content instead of reusing it (same will happen if you wrongly put empty spaces before or after the rendered component).&lt;/p&gt;




&lt;p&gt;If everything worked as expected, the slight moment where your page was empty should have disappeared. The component is now server pre-rendered by Python and React will just kick in on the browser continuing where python left from.&lt;/p&gt;

&lt;p&gt;Congratulations! We achieved our first Isomorphic Application in pure Python!Â :D&lt;/p&gt;

&lt;p&gt;The gist for the code used in this article is available on GitHub: &lt;a href="https://gist.github.com/amol-/4563c7dc27c94d8ea58fabacb4cd71c6"&gt;https://gist.github.com/amol-/4563c7dc27c94d8ea58fabacb4cd71c6&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="https://medium.com/@__amol__"&gt;https://medium.com/@__amol__&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>react</category>
      <category>web</category>
      <category>assets</category>
    </item>
  </channel>
</rss>
