useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people...
Transcript of useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people...
![Page 1: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/1.jpg)
useFlask()useFlask()
![Page 2: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/2.jpg)
useFlask()useFlask()…or how to use a React frontend for
your Flask app
![Page 3: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/3.jpg)
ABOUT MEABOUT MEAdrian Mönnich @ThiefMaster
� Lead Developer of the at CERN
🛠 One of the maintainers
🐍 Python enthusiast since almost 10 years
✨ Moved on from jQuery to React
Indico project
Pallets
![Page 4: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/4.jpg)
STATUS QUOSTATUS QUOBased on what people ask questions about on IRC
WTForms
jQuery
AJAX
JavaScript
![Page 5: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/5.jpg)
WTFORMSWTFORMSEasy for simple cases 🎉
![Page 6: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/6.jpg)
WTFORMSWTFORMSEasy for simple cases 🎉
Complex custom widgets? Welcome to WTF-Forms 🐒
Value round-trip even if validation
fails!
Garbage-in, garbage-out? 🗑
![Page 7: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/7.jpg)
JQUERYJQUERYDo YOU still want to use it? 😜
![Page 8: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/8.jpg)
JQUERYJQUERYDo YOU still want to use it? 😜
Spaghetti code
Classes/IDs all over the
code
Barely maintainable
![Page 9: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/9.jpg)
AJAXAJAXXMLHttpRequest API is horrible
fetch() isn't bad
Are you sending back large chunks of HTML?
Duplicating code for your API?
![Page 10: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/10.jpg)
JAVASCRIPTJAVASCRIPTRarely transpiled (sometimes minified)
Code written for old browsers �
Why are you still writing the same kind of JS
you wrote 10 years ago?
![Page 11: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/11.jpg)
HOW TO IMPROVE IT?HOW TO IMPROVE IT?Build asset bundle with Webpack
Use modern JavaScript (ES6+, ES201x)
✨Make the frontend reactive
![Page 12: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/12.jpg)
No, it's not. Unless you do weird things.Been there, done that.
But it is boilerplate code.
"Configuring webpack is too hard"
![Page 13: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/13.jpg)
Can't we do something like…
from webpack import config
![Page 14: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/14.jpg)
Almost!
It's called create-react-app
Create React App is an officially supported way tocreate single-page React applications. It offers amodern build setup with no configuration.
![Page 15: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/15.jpg)
CREATE-REACT-APPCREATE-REACT-APPContains the whole build config
Auto-rebuild 🏗 & hot reload 🔥
Includes its own dev server
![Page 16: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/16.jpg)
LET'S CODE!LET'S CODE!
��
![Page 17: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/17.jpg)
OUR FLASK APPOUR FLASK APPapp = Flask(__name__) @app.route('/api/time') def time(): return jsonify(now=datetime.now().isoformat()) @app.route('/api/greet/<name>') @app.route('/api/greet-stranger/', defaults={'name': 'mysterious person'}) def greeting(name): msg = 'Welcome, ' + name return jsonify(greeting=msg)
![Page 18: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/18.jpg)
GENERATE THE CLIENTGENERATE THE CLIENT$ npx create-react-app client Creating a new React app in …/ep2019-flask-react/testapp/client. Installing packages. This might take a couple of minutes. Installing react, react-dom, and react-scripts... … We suggest that you begin by typing: cd client npm start Happy hacking!
![Page 19: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/19.jpg)
EXPORT ROUTES TO JSEXPORT ROUTES TO JSHardcoded URLs are ugly! 👎
Especially if they are dynamic 😠
![Page 20: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/20.jpg)
📦 Install some more packages
$ pip install flask-url-map-serializer$ npm install --save-dev flask-urls.macro$ npm install --save flask-urls
![Page 21: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/21.jpg)
🎣 Hook up the build scripts with Flask
// client/.babel-plugin-macrosrc.jsconst {execSync} = require('child_process'); const urlMap = JSON.parse(execSync('flask url_map_to_json')); module.exports = { flaskURLs: { urlMap } };
![Page 22: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/22.jpg)
🔗 Link the dev servers
// client/src/setupProxy.jsconst proxy = require('http-proxy-middleware'); module.exports = app => { app.use(proxy('/api', { target: process.env.FLASK_URL || 'http://127.0.0.1:5000' })); };
![Page 23: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/23.jpg)
RECAPRECAPWe now have:
Our Flask app providing an API
Boilerplate for a React frontend
The ability to build Flask URLs in JS
The CRA dev server forwarding /api/* to
Flask
![Page 24: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/24.jpg)
// client/src/App.js
import Demo from './Demo';
<Demo />
1// …2
3 4function App() {5 return (6 {/* … */}7 <header classname="App-header">8 <img src={logo} className="App-logo" alt="logo" />9
10 </header>11 {/* … */}12 );13}14
![Page 25: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/25.jpg)
// client/src/Demo.js
import flask from 'flask-urls.macro';
const timeURL = flask`time`;const greetingURL = flask`greeting`;
1import React, {useState, useEffect} from 'react';2
3 4
56
7export default function Demo() {8 // continued on next slide…9}10
![Page 26: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/26.jpg)
const [time, setTime] = useState(null);useEffect(() => { (async () => { const resp = await fetch(timeURL()); const data = await resp.json(); setTime(data.now); })();}, []); const url = greetingURL({name: 'snake'});return time && ( <p>It's {time} and your greeting URL is {url}</p>);
123456789
10111213
![Page 27: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/27.jpg)
How the hell does this all work?! �
![Page 28: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/28.jpg)
How the hell does this all work?! �
![Page 29: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/29.jpg)
How the hell does this all work?! �
// Babel macro - runs at build time!import flask from 'flask-urls.macro'; // Tagged template compiles to functionconst greetingURL = flask`greeting`; // equivalent of url_for('greeting', name='snake')const url = greetingURL({name: 'snake'});
![Page 30: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/30.jpg)
How the hell does this all work?! �
// Babel macro - runs at build time!import flask from 'flask-urls.macro'; // Tagged template compiles to functionconst greetingURL = flask`greeting`; // equivalent of url_for('greeting', name='snake')const url = greetingURL({name: 'snake'});
![Page 31: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/31.jpg)
How the hell does this all work?! �
// Babel macro - runs at build time!import flask from 'flask-urls.macro'; // Tagged template compiles to functionconst greetingURL = flask`greeting`; // equivalent of url_for('greeting', name='snake')const url = greetingURL({name: 'snake'});
![Page 32: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/32.jpg)
That's the code generated by the macro
const greetingURL = flask_urls__WEBPACK_IMPORTED_MODULE_1___default.a.bind( null, { endpoint: "greeting", rules: [/* …building rules omitted here… */] }, "" );
![Page 33: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/33.jpg)
YOU CAN CONTRIBUTE!YOU CAN CONTRIBUTE!
We could really use…
🧾 Docs (only examples right now 😞)
✨ TypeScript stubs
🖥 More projects using it (and finding bugs? 🐛)
➡ Brag about having contributed to a CERNproject! 🎉
indico/js-flask-urls
![Page 34: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/34.jpg)
OTHER COOL STUFFOTHER COOL STUFF
![Page 35: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/35.jpg)
Non-macro Babel plugin for Flask URL building
Same features as the macro version
Alternative to the macro, but requires control
over the Webpack/Babel config
import greetingURL from 'flask-url:greeting'; import otherURL from 'flask-url:blueprint.endpoint';
![Page 36: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/36.jpg)
Server-side validation:
Form handling & validation:
webargs
🏁 react-final-form
![Page 37: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/37.jpg)
More complete example of React-Flask integration
Also covers how to build for production
ThiefMaster/flask-cra-example
![Page 38: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/38.jpg)
FIND ME ON…FIND ME ON… @ThiefMaster
ThiefMaster @ Freenode (#pocoo, #indico)
![Page 39: useFlask()...Moved on from jQuery to React Indico project Pallets STATUS QUO Based on what people ask questions about on IRC WTForms jQuery AJAX JavaScript WTFORMS Easy for simple](https://reader033.fdocuments.net/reader033/viewer/2022060415/5f134fae9329342c9004621f/html5/thumbnails/39.jpg)
FIND ME ON…FIND ME ON… @ThiefMaster
ThiefMaster @ Freenode (#pocoo, #indico)
…or right here! 🐍
I have hexagonal stickers!…and my team at CERN is looking for a student intern!