Code Monkey home page Code Monkey logo

react-python's Introduction

NOTE: This project is no longer actively maintained.

PyReact

PyReact is a Python wrapper around the React JavaScript library and JSX.

Specifically, it provides an API to transform JSX files into JavaScript from within your Python application, as well as providing access to the latest React build.

Installation

PyPI: PyReact is hosted on PyPI, and can be installed with pip:

$ pip install PyReact

Alternatively, add it into your requirements file:

PyReact==0.6.0

Dependencies: PyReact uses PyExecJS to execute the bundled React code, which requires that a JS runtime environment is installed on your machine. We don't explicitly set a dependency on a runtime environment; Mac OS X comes bundled with one. If you're on a different platform, we recommend PyV8.

Usage

Transform your JSX files via the provided jsx module::

from react import jsx

# For multiple paths, use the JSXTransformer class.
transformer = jsx.JSXTransformer()
for jsx_path, js_path in my_paths:
    transformer.transform(jsx_path, js_path=js_path)

# For a single file, you can use a shortcut method.
jsx.transform('path/to/input/file.jsx', js_path='path/to/output/file.js')

You can also use transform_string(jsx) method to transform strings:

from react import jsx
transformer = jsx.JSXTransformer()
js = transformer.transform_string(jsx)

Django: PyReact includes a JSX compiler for django-pipeline. It has been tested with django-pipeline 1.3.20, but may work with other versions too. Add it to your project's pipeline settings like this:

PIPELINE_COMPILERS = (
  'react.utils.pipeline.JSXCompiler',
)

License

Copyright (c) 2013 Facebook, Inc. Released under the Apache License, Version 2.0.

react-python's People

Contributors

andreypopp avatar douglas-vaz avatar jonathanraiman avatar kmeht avatar marioidival avatar richardkiss avatar rmoch avatar sophiebits avatar zpao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-python's Issues

Python3 version

Hello,

Is there any plans for Python3 version of react-python? Thanks.

Best practices for passing context to React

Hey guys,
I tried to get some support on the IRC channel but couldn't find anyone using PyReact.

My main question is that in my project right now we are passing in a context variable to render our templates.

Is there a recommended way to pass context data to React.js so that it can render components with that data?

Feel free to email me if you would rather not have this one the github page.

Fails to build: django-pipeline==1.3.24

Hi there, firstly thanks fro this useful plugin!

I'm having problems getting it working with django-pipeline however collectstatic renders the following:

Post-processed '/Users/rosscdh/Projects/my_app/toolkit/static/js/matter_search.js' as '/Users/rosscdh/Projects/my_app/toolkit/static/js/matter_search.js'

0 static files copied to '/Users/rosscdh/Projects/my_app/toolkit/static', 13115 unmodified, 1 post-processed.

The paths appear incorrect as it should be referencing matter_search.jsx (as in the settings.py files below.

Tried variations of:

PIPELINE_JS = {
    'reactjs': {
        'source_filenames': (
          '{SITE_ROOT}/toolkit/apps/matter/static/js/matter_search.jsx'.format(SITE_ROOT=SITE_ROOT),
        ),
        'output_filename': '{STATIC_ROOT}/js/matter_search.js'.format(STATIC_ROOT=STATIC_ROOT),
    }
}
PIPELINE_COMPILERS = (
  'react.utils.pipeline.JSXCompiler',
)

And

PIPELINE_JS = {
    'reactjs': {
        'source_filenames': (
          'toolkit/apps/matter/static/js/matter_search.jsx',
        ),
        'output_filename': 'toolkit/apps/matter/static/js/matter_search.js',
    }
}
PIPELINE_COMPILERS = (
  'react.utils.pipeline.JSXCompiler',
)

However, it works perfectly if I create a custom management command and issue manual JSX transform.

I seem to have missed something or am overlooking a tree in the forest.

Thanks in advance

Update React to v0.12.0

React v0.120 is releases. Please update to last version.

BTW, this project does not include react.js and the others. did you add that to the .gitignore file ?

React.renderComponentToString support

It would be really nice if PyReact could offer a generic WSGI app like the react-middleware project or an API where one could give it a path to a React component and it will return a string so the controller can include it to the response string.

transform() doesn't pass *args (only **kwargs)

The documented example of:

jsx.transform('path/to/input/file.jsx', 'path/to/output/file.js')

Does not work. I think the transform functions should be changed as below.

I think transform functions should be:

def transform(jsx_path, *args, **opts):
    return JSXTransformer().transform(jsx_path, *args, **opts)

def transform_string(jsx, *args, **opts):
    return JSXTransformer().transform_string(jsx, *args, **opts)

Exception attribute 'error' doesn't exist

When handling an execjs ProgramError, the code attempts to read the message attribute from the raised exception. The exception doesn't have any such attribute (causing a nested exception).

I'm not sure what the right fix is. I worked around be just doing str(e), instead of e.message which at least unblocks me.

Node should be added to the requirements and note that it is the only runtime that works for Windows 7.

Windows 7 64-bit
pyexecjs 1.1.0
pyv8 1.0
NodeJS 4.0.0

Using execjs with pyv8 (it seems that PyV8 takes precedence):

import execjs

execjs.get().name
Out[2]: u'PyV8'

from react import jsx
jsx.transform('helloworld.jsx')
Traceback (most recent call last):

  File "<ipython-input-4-ba80c49e788e>", line 1, in <module>
    jsx.transform('helloworld.jsx')

  File "C:\Python27\python-2.7.6.amd64\lib\site-packages\react\jsx.py", line 64, in transform
    return JSXTransformer().transform(jsx_path, **opts)

  File "C:\Python27\python-2.7.6.amd64\lib\site-packages\react\jsx.py", line 51, in transform
    js = self.transform_string(i.read(), **kwargs)

  File "C:\Python27\python-2.7.6.amd64\lib\site-packages\react\jsx.py", line 45, in transform_string
    raise TransformError(e.message[7:])

TransformError: ()

'JScript' was my original runtime:

import os
os.environ['EXECJS_RUNTIME'] = 'JScript'
jsx.transform('helloworld.jsx')
Traceback (most recent call last):

  File "<ipython-input-7-ba80c49e788e>", line 1, in <module>
    jsx.transform(helloworld.jsx')

  File "C:\Python27\python-2.7.6.amd64\lib\site-packages\react\jsx.py", line 64, in transform
    return JSXTransformer().transform(jsx_path, **opts)

  File "C:\Python27\python-2.7.6.amd64\lib\site-packages\react\jsx.py", line 51, in transform
    js = self.transform_string(i.read(), **kwargs)

  File "C:\Python27\python-2.7.6.amd64\lib\site-packages\react\jsx.py", line 45, in transform_string
    raise TransformError(e.message[7:])

TransformError: or: 'undefined' is null or not an object

But installing NodeJS and using it as the runtime works:

os.environ['EXECJS_RUNTIME'] = 'Node'

In [9]: jsx.transform('helloworld.jsx')
Out[9]: u'var HelloMessage = React.createClass({displayName: \'HelloMessage\',\n  render: function() {\n    return React.createElement("div", null, "Hello ", this.props.name);\n  }\n});\n\nReact.render(React.createElement(HelloMessage, {name: "John"}), mountNode);'

So I guess that should be added to the readme. I have uninstalled PyV8 and now Node is the default runtime under execjs.

On Windows with pipeline 1.4.6, cannot find files due to quotes

  File "C:\wc\...\lib\site-packages\react\jsx.py", line 50, in transform
    with open(jsx_path, 'rU') as i:
IOError: [Errno 22] invalid mode ('rU') or filename: u"'C:\\...\\staticfiles\\jsx\\my_jsx_file.jsx'"

Single quotes (') surround the jsx_path and js_path sent in from pipeline, as you can see from the error message thrown above. It comes from their shlex.quote call here: https://github.com/cyberdelia/django-pipeline/blob/master/pipeline/compilers/__init__.py#L43

I currently hacked around it by replacing the transform(...) in C:\wc...\lib\site-packages\react\jsx.py with this:

def transform(self, jsx_path, js_path=None, **kwargs):
    if jsx_path.startswith("'"):
        jsx_path = jsx_path[1:-1]
    with open(jsx_path, 'rU') as i:
        js = self.transform_string(i.read(), **kwargs)
        if js_path:
            if js_path.startswith("'"):
                js_path = js_path[1:-1]
            with open(js_path, 'wb') as o:
                o.write(js.encode('utf8'))
        return js

jsx.transform takes only one argument

In README.md you wrote:

# For a single file, you can use a shortcut method.
jsx.transform('path/to/input/file.jsx', 'path/to/output/file.js')

But jsx.transform takes only one argument:

def transform(jsx_path, **opts):
    return JSXTransformer().transform(jsx_path, **opts)

Regards,
Henri

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.