Skip to main content

A zero-fuss way to create non single page apps with react.

Project description

React Pages

A zero-fuss way to create non single page apps with react.
  • Zero Configuration required. Mostly thanks to create-react-app.
  • Custom react scripts inbuilt.
  • Allows cross-page imports (create-react-app doesn't allow this).
  • Ready-to-serve production builds with the proper paths. (using --static-url option)
  • Natively use react in django.
  • Go from development to production with ease.
  • Donwloads npm packages only once, per virtualenv.
    This means creating a new project is really fast (at the cost of installation time).

Terminology

Project

The project contains the node.js modules necessary to use react and the pages you create.

└── my_project
    ├── package.json
    ├── package-lock.json
    ├── .env
    ├── .gitignore
    ├── public
    │   ├── favicon.ico
    │   ├── index.html
    │   └── manifest.json
    <pages>

Page:

A page is a directory containing at least an index.js file, (and other css/js files specific to your application.)

└── my_page
    ├── App.css
    ├── App.js
    ├── App.test.js
    ├── index.css
    ├── index.js
    ├── logo.svg
    └── registerServiceWorker.js

Install

PyPI version (tag)

pip install react-pages

License: MIT License (MIT)
Requires: Python >=3.6


If you don't have node,

For bash, use nvm.

For fish shell, you can use fisher fnm (get fisher).

Once you have npm/node, react pages will work as expected.

TODO: make react-pages automatically install node

Commands

$ react-pages project my_project # create a "project"

$ cd my_project # don't forget to do this!

$ react-pages page my_page # create a "page"

$ react-pages develop # development

$ react-pages deploy # production

# Open `./my_project/build/my_page/index.html` in browser


$ react-pages runserver # django runserver alternative

# installs the required npm packages, once and for all.
# automatically run when required.
$ react-pages cache

# removes npm packages
$ react-pages clear-cache

Django Integration

Remember to use react-pages runserver instead of manage.py runserver!

(This was done to remove the manual build step).

Setup

settings.py

INSTALLED_APPS = [
    ...
    'react_pages',
    ...
]

# specify the react-pages project directory
REACT_PAGES_PROJECT_DIR = os.path.join(BASE_DIR, 'my_project')

STATICFILES_DIRS = [
    ...
    os.path.join(REACT_PAGES_PROJECT_DIR, 'build')  # mark the build dir as a static file dir
    ...
]

Usage

template.html

{% load react_pages %}
...
{% render_react_page 'my_page' %}
...

That's it!
React Pages will pick-up the "my_page" page from "my_project" project and do the necessary work to transpile react JSX.

Django Context

You can pass django template context varialbes like so -

views.py

context['py_var'] = [1, 2, 3]

template.html

{% render_react_page 'my_page' js_var=py_var %}

App.js

console.log(js_var);

Note: These must be JSON serializable or JSON serialized.

Class Based View

views.py

from react_pages.views import ReactPageView

Class MyPageView(ReactPageView):
    page_name = 'my_page'

urls.py

urlpatterns = [
    ...
    path('my_page/', views.MyPageView.as_view(), name="my page"),
]

when you go over to 'my_page/' url, you'll see the react page rendered in its full glory!

To pass the a context to JS, define a get_js_context() method

views.py

Class MyPageView(ReactPageView):
    page_name = 'my_page'

    def get_js_context(self):
        return {'js_var': 'Hello!'}

App.js

console.log(js_var);

For production, just put DEBUG=False in settings.py and relax.

Note: This is not implemented yet.

Existing projects

React Pages will automatically patch itsef into any existing project, that was created using create-react-app.

Just run react-pages project . from your project directory!

Projects not using create-react-app will probably work, but no guarantees can be made.

Issues


Buy Me A Coffee

🐍🏕️

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

react-pages-0.2.4.tar.gz (137.5 kB view details)

Uploaded Source

Built Distribution

react_pages-0.2.4-py2.py3-none-any.whl (153.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file react-pages-0.2.4.tar.gz.

File metadata

  • Download URL: react-pages-0.2.4.tar.gz
  • Upload date:
  • Size: 137.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for react-pages-0.2.4.tar.gz
Algorithm Hash digest
SHA256 f8f138a7a23efc705d8ee1304c41921a684f7f5196d2edd3a3f126f83a8a754e
MD5 7ff0d1f1e08a06bf7eb6a4c8036afc5c
BLAKE2b-256 d18dfc4277239cbaea617b0329b8a576f895223c8c413290185385fc8dc0faea

See more details on using hashes here.

File details

Details for the file react_pages-0.2.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for react_pages-0.2.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 aa87a56e18659544faf4666eebe631257a42ebfdede52062807d3e844dc23c54
MD5 a0721cae57f8fae741354dc97a1c2186
BLAKE2b-256 35f9acc4e0e4366ea19d369db287b9eaaf10827bec8b860950bd484e99d6b19c

See more details on using hashes here.

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