forked from markfinger/python-react
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
78 lines (57 loc) · 1.65 KB
/
example.py
File metadata and controls
78 lines (57 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import json
from flask import Flask, render_template, request, redirect, jsonify
from react.conf import settings as react_settings
from react.render import render_component
from webpack.conf import settings as webpack_settings
from webpack.compiler import webpack
DEBUG = True
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# As a convenience for development, only connect to the
# render server when DEBUG is False
react_settings.configure(RENDER=not DEBUG)
webpack_settings.configure(
STATIC_ROOT=os.path.join(BASE_DIR, 'static'),
STATIC_URL='/static/',
WATCH=DEBUG,
HMR=DEBUG,
CONFIG_DIRS=BASE_DIR,
CONTEXT={
'DEBUG': DEBUG,
},
)
app = Flask(__name__)
app.debug = DEBUG
comments = []
@app.route('/')
def index():
config_file = os.path.join(BASE_DIR, 'example.webpack.js')
component = os.path.join(BASE_DIR, 'app', 'CommentBox.jsx')
props = {
'comments': comments,
'url': '/comment/',
}
rendered = render_component(component, props)
webpack_context = {
'component': component,
'props_var': 'window.mountProps',
'container': 'mount-container',
}
bundle = webpack(config_file, context=webpack_context)
return render_template(
'index.html',
bundle=bundle,
webpack_context=webpack_context,
rendered=rendered,
)
@app.route('/comment/', methods=('POST',))
def comment():
comments.append({
'name': request.form['name'],
'text': request.form['text'],
})
if request.is_xhr:
return jsonify(comments=comments)
return redirect('/')
if __name__ == '__main__':
app.run()