forked from markfinger/python-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
56 lines (41 loc) · 1.45 KB
/
Copy pathexample.py
File metadata and controls
56 lines (41 loc) · 1.45 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
import os
from flask import Flask, render_template, request, redirect, jsonify
from react.render import render_component
from settings import DEBUG, BASE_DIR
app = Flask(__name__)
app.debug = DEBUG
comments = []
@app.route('/')
def index():
no_js = 'no-js' in request.args
comment_box = render_component(
# An absolute path to the component
os.path.join(BASE_DIR, 'static', 'jsx', 'CommentBox.jsx'),
# The data that we use to render the component and mount it
# on the client-side
props={
'comments': comments,
'url': '/comment/',
'pollInterval': 2000,
},
# Ensure that the source code is translated to JS from JSX & ES6/7.
# This enables us to use future-facing JS across the client-side
# and server-side
translate=True,
# If you do not intend to use React on the client-side, rendering
# to static markup will optimise the generated markup
to_static_markup=no_js
)
return render_template('index.html', comment_box=comment_box, no_js=no_js)
@app.route('/comment/', methods=('GET', 'POST'))
def comment():
if request.method == 'POST':
comments.append({
'author': request.form['author'],
'text': request.form['text'],
})
if not request.is_xhr:
return redirect('/?no-js')
return jsonify(comments=comments)
if __name__ == '__main__':
app.run()