SlideShare a Scribd company logo
CREATING A FULL STACK WEB APP WITH
PYTHON, NPM, WEBPACK AND REACT
by Angela Branaes
Creating a full stack web app with python, npm, webpack and react
Creating a full stack web app with python, npm, webpack and react
Creating a full stack web app with python, npm, webpack and react
Creating a full stack web app with python, npm, webpack and react
THE STRUCTURE OF A WEB APP
➤ Separated into front and back end.
➤ Multiple technologies.
➤ Front end is:
➤ Look and feel.
➤ User interaction.
➤ Back end is:
➤ Long running operations.
➤ Data, and data processing.
➤ Application logic.
INITIAL PROJECT SETUP
➤ Create the following directory structure
.
├── README.md
└── fullstack_template/
├── server/
└── static/
├── css/
├── dist/
├── images/
└── js/
$ cd fullstack_template/static
WE’RE GOING TO NEED A JAVASCRIPT PACKAGE MANAGER
➤ Using a package manager makes it painless to:

➤ Keep your project dependencies up to date.

➤ Fetch and install new packages.

➤ Similar to brew, pip, pacman etc.
NPM
NPM = NODE PACKAGE MANAGER
➤ Automatically included with Node.js

➤ Easy to use

➤ well documented

➤ nearly 500 000 packages available

➤ Initialise NPM:

$ npm init
➤ This creates a file called package.json
OUR UPDATED DIRECTORY TREE
.
├── README.md
└── fullstack_template/
├── server/
└── static/
├── css/
├── dist/
├── images/
├── js/
└── package.json
THE PURPOSE OF PACKAGE.JSON
1. Keeps track of dependencies and versions.

2. Informs other developers about your project.

3. Makes installing, running and updating a project automatic and reproducible
THIS IS MY PACKAGE.JSON
{
"name": "FullStackTemplate",
"version": "1.0.0",
"description": "Fullstack Template",
"main": "index.jsx",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
},
"keywords": [
"python",
"react",
"npm",
"webpack"
],
"author": "Angela Branaes",
"license": "MIT",
"devDependencies": {
"webpack": "^3.0.0"
}
}
WHY WE NEED A MODULE BUNDLER
➤ Minimises the number of <script> tags in your HTML
➤ This means faster loading, and less hassle.
➤ Don’t need to worry about bundle import ordering!
➤ Don’t have to use globals or namespacing.
➤ Lazy module loading.
➤ We will be using Webpack.
WEBPACK
WEBPACK
➤ Only understands JavaScript.
➤ Everything else needs a loader or plugin.
➤ Everything is a module.
➤ You can require() JavaScript, React, CSS or images.
➤ Creates a dependency tree of all your required modules, and packages those into
bundles.
➤ Easy to get started with, if you only need JavaScript.
➤ Minor learning curve for css/less and image loading.
➤ Lets you split your bundles and load them asynchronously and lazily.
➤ Integrates seamlessly with npm.
INSTALL & CONFIGURE WEBPACK
➤ Install webpack:
$ npm i webpack --save-dev
➤ Add a file named webpack.config.js:
const webpack = require(‘webpack');
const config = {
entry: __dirname + '/js/index.jsx',
output: {
path: __dirname + '/dist',
filename: 'bundle.js',
},
resolve: {
extensions: ['.js', '.jsx', '.css']
},
};
module.exports = config;
OUR UPDATED DIRECTORY TREE
.
├── README.md
└── fullstack_template/
├── server/
└── static/
├── css/
├── dist/
├── images/
├── js/
├── package.json
└── webpack.config.js
SINGLE VS MULTI PAGE APPLICATIONS
➤ I mostly use Webpack for single page applications.
➤ You can also use webpack for multi page applications:
entry: {

“indexPage”: __dirname + “js/index.jsx”,

“aboutPage”: __dirname + “js/about.jsx”
},
output: {
path: __dirname + “/dist”,

filename: “[name].js” //e.g. aboutPage.js
}
LET’S DISPLAY AN ALERT!
OUR UPDATED DIRECTORY TREE
.
├── README.md
└── fullstack_template/
├── server/
└── static/
├── css/
├── dist/
│ └── bundle.js
├── images/
├── js/
│ └── index.jsx
├── index.html
├── package.json
└── webpack.config.js
CREATE INDEX.HTML
➤ A really simple index.html is all you need.
➤ The index.html just loads your JavaScript bundle
<body>
<div id="content" />
<script src="dist/bundle.js" type=“text/javascript”>
</script>
</body>
➤ All the layout and behaviour live in the JS/React files!
CREATE INDEX.JSX
➤ Just 1 line of Plain Old JavaScript
alert(“Hello World!”);
BUILDING YOUR APPLICATION
➤ Create run commands to simplify building your code.
➤ Run commands are just aliases.
➤ Example from my package.json:
“scripts": {
"watch": "webpack --progress -d --config webpack.config.js —watch"
}
➤ Makes the dev process more fluid.
➤ I always add the following:
➤ build
➤ dev-build
➤ watch
START THE WATCH COMMAND
➤ Any changes get built automatically
$ npm run watch
➤ Open index.html….
Creating a full stack web app with python, npm, webpack and react
CREATING A SIMPLE REACT APP
THE BENEFITS OF USING REACT
➤ Easy to build UIs composed of small, distinct components.
➤ Encourages design of reusable UI components.
➤ Easier to understand what’s going on when markup lives with the code (JSX).
➤ Automatic re-rendering of components on change.
➤ Easy to maintain.
INSTALL REACT
$ npm i react react-dom —save-dev
REACTIFY INDEX.JSX
import React from "react";
import ReactDOM from "react-dom";
import App from “./App";
ReactDOM.render(<App />, document.getElementById("content"));
ADD A REACT “APP” CLASS
➤ Remember to export your react classes!
// App.jsx
import React from “react”;
export default class App extends React.Component {
render () {
return <p> Hello React!</p>;
}
}
OUR UPDATED DIRECTORY TREE
.
├── README.md
└── fullstack_template/
├── server/
└── static/
├── css/
├── dist/
├── images/
├── js/
│ ├── index.jsx
│ └── App.jsx
├── index.html
├── package.json
└── webpack.config.js
WHAT IS JSX?
➤ Syntax extension to JavaScript.
➤ Optimised on compilation, so faster than JavaScript.
➤ Statically typed and mostly type-safe. JavaScript is not.
➤ Lets you write HTML tags in your JavaScript functions:
<Hello name=“Rimini” />
instead of
React.createElement(Hello, {name: “Rimini}, null)
➤ Recognises upper-case element-types as written in React. E.g. <Hello />
➤ How do we make our browser understand JSX?
INTRODUCING BABEL
Write next-generation javascript right now!
Transform JSX to JS.
ADD BABEL
➤ Install Babel
➤ Add the Babel presets to the package.json:
“babel”: {
“presets”: [
“es2015”,
“react”
]
},
ADD A BABEL-LOADER RULE TO THE WEBPACK CONFIG:
module: {
rules: [
{
test: /.jsx?/,
exclude: /node_modules/,
use: 'babel-loader'
}
]
}
OPEN INDEX.HTML IN YOUR BROWSER
➤ This should now show the “Hello React” paragraph we added in our new React
App.jsx file.
Creating a full stack web app with python, npm, webpack and react
PYTHON FLASK SERVER
PYTHON
➤ Go to the server folder
➤ Ensure you’re in a Python virtualenv
$ mkvirtualenv fullstack
➤ Install Flask
(fullstack)$ pip install flask
➤ Create a file called server.py in the server folder
OUR UPDATED DIRECTORY TREE
.
├── README.md
└── fullstack_template/
├── server/
│ └── server.py
└── static/
├── css/
├── dist/
├── images/
├── js/
│ ├── index.jsx
│ └── App.jsx
├── index.html
├── package.json
└── webpack.config.js
SERVER.PY
from flask import Flask, render_template
app = Flask(__name__, static_folder=“../static/dist",
template_folder="../static")
@app.route("/")
def index():
return render_template("index.html")
@app.route("/hello")
def hello():
return "Hello World!”
if __name__ == "__main__":
app.run()
START THE SERVER
➤ Go to: http://localhost:5000/
$ python server.py
REQUEST INFO FROM THE SERVER
var $ = require(‘jquery');
getPythonHello() {
$.get(window.location.href + 'hello', (data) => {
console.log(data);
this.personaliseGreeting(data);
});
}
MAKE PYTHON DO SOMETHING MORE INTERESTING
➤ We call get_hello() whenever we hit the /hello endpoint
def get_hello():
greeting_list = ['Ciao', 'Hei', 'Salut', 'Hola', 'Hallo', 'Hej']
return random.choice(greeting_list)
SAY HI TO SOMEONE SPECIAL
➤ There should be a class for that!

➤ Change the App.jsx render method to the following:

render () {
return (
<PageHeader>
<div className='header-contents'>
<Hello name='Rimini' />
</div>
</PageHeader>
);
)
OUR UPDATED DIRECTORY TREE
.
├── README.md
└── fullstack_template/
├── server/
│ └── server.py
└── static/
├── css/
├── dist/
├── images/
├── js/
│ ├── index.jsx
│ ├── App.jsx
│ └── Hello.jsx
├── index.html
├── package.json
└── webpack.config.js
CREATE THE HELLO CLASS
export default class Hello extends React.Component {
constructor(props) {
super(props);
// greeting is now “Hello Rimini”
this.state = {greeting: 'Hello ' + this.props.name};
// This binding is necessary to make `this`
// work in the button callback
this.getPythonHello = this.getPythonHello.bind(this);
}
}
➤ Add this function to the Hello class:
➤ This will re-render the greeting on our website to a new one when called.
personaliseGreeting(greeting) {
this.setState({greeting: greeting + ' ' + this.props.name + '!'});
}
LET’S FINALLY RENDER OUR HELLO!
render () {
return (
<h1>{this.state.greeting}</h1>
<hr/>
<Button onClick={this.getPythonHello}>
Say Hello!
</Button>
);
}
Creating a full stack web app with python, npm, webpack and react
Creating a full stack web app with python, npm, webpack and react
CREATE A NICE LAYOUT WITH CSS
➤ Webpack ONLY understands JavaScript.
➤ Install the following loaders and plugins:
➤ style-loader
➤ css-loader
➤ extract-text-webpack-plugin
➤ Add a plugin to the webpack config:
plugins: [
new ExtractTextPlugin('styles.css'),
]
OUR UPDATED DIRECTORY TREE
.
├── README.md
└── fullstack_template/
├── server/
│ └── server.py
└── static/
├── css/
│ └── fullstack.css
├── dist/
│ ├── bundle.js
│ └── styles.css
├── images/
├── js/
│ ├── index.jsx
│ ├── App.jsx
│ └── Hello.jsx
├── index.html
├── package.json
└── webpack.config.js
➤ Add fullstack.css to the css folder
➤ Add a few css rules for our header
➤ require fullstack.css in App.jsx:
require(‘../css/fullstack.css');
➤ Add the bundled css to the index.html:
<link rel="stylesheet" href="dist/styles.css">
Creating a full stack web app with python, npm, webpack and react
OUR UPDATED DIRECTORY TREE
.
├── README.md
└── fullstack_template/
├── server/
│ └── server.py
└── static/
├── css/
│ └── fullstack.css
├── dist/
│ ├── bundle.js
│ └── styles.css
├── images/
│ └── header.png
├── js/
│ ├── index.jsx
│ ├── App.jsx
│ └── Hello.jsx
├── index.html
├── package.json
└── webpack.config.js
ADD A NICE BACKGROUND IMAGE
1. Add the file-loader loader
2. Add an image to your images/ folder
3. Set the image to be your header background in the fullstack.css file
background-image: url(‘../images/header.jpg’);
4. NOTE: you have to load the image in your React app for it to show up!
IN APP.JSX
import HeaderBackgroundImage from ‘../images/header.jpg’;
➤ Add this fn to your class:
addHeaderImg() {
let headerBg = new Image();
headerBg.src = HeaderBackgroundImage;
}
➤ And this to your render():
{this.addHeaderImg()}
Creating a full stack web app with python, npm, webpack and react
Creating a full stack web app with python, npm, webpack and react
Creating a full stack web app with python, npm, webpack and react
Creating a full stack web app with python, npm, webpack and react
THANK YOU! ANY QUESTIONS?
@angineering
@angineering - The code is on GitHub:
https://github.com/angineering/FullStackTemplate
@angineering - This talk is also a blogpost
Ad

Recommended

PHP SA 2014 - Releasing Your Open Source Project
PHP SA 2014 - Releasing Your Open Source Project
xsist10
 
BaláZs Ree Introduction To Kss, Kinetic Style Sheets
BaláZs Ree Introduction To Kss, Kinetic Style Sheets
Vincenzo Barone
 
Developers, Be a Bada$$ with WP-CLI
Developers, Be a Bada$$ with WP-CLI
WP Engine
 
Martin Aspeli Extending And Customising Plone 3
Martin Aspeli Extending And Customising Plone 3
Vincenzo Barone
 
Jenkins Setup Document
Jenkins Setup Document
mobi fly
 
D installation manual
D installation manual
Faheem Akbar
 
2015 - Basta! 2015, DE: JavaScript und build
2015 - Basta! 2015, DE: JavaScript und build
Daniel Fisher
 
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
Brian Hogg
 
Vagrant crash course
Vagrant crash course
Marcus Deglos
 
WPDay Bologna 2013
WPDay Bologna 2013
Danilo Ercoli
 
Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...
Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...
Acquia
 
Godefroid Chapelle Ajax With Plone 3 Kss Development Patterns
Godefroid Chapelle Ajax With Plone 3 Kss Development Patterns
Vincenzo Barone
 
BPMS1
BPMS1
tutorialsruby
 
Hitchhiker's guide to the front end development
Hitchhiker's guide to the front end development
정윤 김
 
Front end performance optimization
Front end performance optimization
Stevie T
 
Vagrant
Vagrant
Michael Peacock
 
Front End Performance
Front End Performance
Konstantin Käfer
 
GlassFish Embedded API
GlassFish Embedded API
Eduardo Pelegri-Llopart
 
Vagrant for real codemotion (moar tips! ;-))
Vagrant for real codemotion (moar tips! ;-))
Michele Orselli
 
Introduction to Webpack - Ordina JWorks - CC JS & Web
Introduction to Webpack - Ordina JWorks - CC JS & Web
JWORKS powered by Ordina
 
Vagrant for Virtualized Development
Vagrant for Virtualized Development
Adam Culp
 
Rock-solid Magento Development and Deployment Workflows
Rock-solid Magento Development and Deployment Workflows
AOE
 
Front end performance tip
Front end performance tip
Steve Yu
 
WebPagetest Power Users - Velocity 2014
WebPagetest Power Users - Velocity 2014
Patrick Meenan
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
Edureka!
 
#3 Hanoi Magento Meetup - Part 2: Scalable Magento Development With Containers
#3 Hanoi Magento Meetup - Part 2: Scalable Magento Development With Containers
Hanoi MagentoMeetup
 
Single page apps with drupal 7
Single page apps with drupal 7
Chris Tankersley
 
Pyramid Deployment and Maintenance
Pyramid Deployment and Maintenance
Jazkarta, Inc.
 
WEBPACK
WEBPACK
home
 
How to Webpack your Django!
How to Webpack your Django!
David Gibbons
 

More Related Content

What's hot (20)

Vagrant crash course
Vagrant crash course
Marcus Deglos
 
WPDay Bologna 2013
WPDay Bologna 2013
Danilo Ercoli
 
Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...
Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...
Acquia
 
Godefroid Chapelle Ajax With Plone 3 Kss Development Patterns
Godefroid Chapelle Ajax With Plone 3 Kss Development Patterns
Vincenzo Barone
 
BPMS1
BPMS1
tutorialsruby
 
Hitchhiker's guide to the front end development
Hitchhiker's guide to the front end development
정윤 김
 
Front end performance optimization
Front end performance optimization
Stevie T
 
Vagrant
Vagrant
Michael Peacock
 
Front End Performance
Front End Performance
Konstantin Käfer
 
GlassFish Embedded API
GlassFish Embedded API
Eduardo Pelegri-Llopart
 
Vagrant for real codemotion (moar tips! ;-))
Vagrant for real codemotion (moar tips! ;-))
Michele Orselli
 
Introduction to Webpack - Ordina JWorks - CC JS & Web
Introduction to Webpack - Ordina JWorks - CC JS & Web
JWORKS powered by Ordina
 
Vagrant for Virtualized Development
Vagrant for Virtualized Development
Adam Culp
 
Rock-solid Magento Development and Deployment Workflows
Rock-solid Magento Development and Deployment Workflows
AOE
 
Front end performance tip
Front end performance tip
Steve Yu
 
WebPagetest Power Users - Velocity 2014
WebPagetest Power Users - Velocity 2014
Patrick Meenan
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
Edureka!
 
#3 Hanoi Magento Meetup - Part 2: Scalable Magento Development With Containers
#3 Hanoi Magento Meetup - Part 2: Scalable Magento Development With Containers
Hanoi MagentoMeetup
 
Single page apps with drupal 7
Single page apps with drupal 7
Chris Tankersley
 
Pyramid Deployment and Maintenance
Pyramid Deployment and Maintenance
Jazkarta, Inc.
 
Vagrant crash course
Vagrant crash course
Marcus Deglos
 
Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...
Fast Paced Drupal 8: Accelerating Development with Composer, Drupal Console a...
Acquia
 
Godefroid Chapelle Ajax With Plone 3 Kss Development Patterns
Godefroid Chapelle Ajax With Plone 3 Kss Development Patterns
Vincenzo Barone
 
Hitchhiker's guide to the front end development
Hitchhiker's guide to the front end development
정윤 김
 
Front end performance optimization
Front end performance optimization
Stevie T
 
Vagrant for real codemotion (moar tips! ;-))
Vagrant for real codemotion (moar tips! ;-))
Michele Orselli
 
Introduction to Webpack - Ordina JWorks - CC JS & Web
Introduction to Webpack - Ordina JWorks - CC JS & Web
JWORKS powered by Ordina
 
Vagrant for Virtualized Development
Vagrant for Virtualized Development
Adam Culp
 
Rock-solid Magento Development and Deployment Workflows
Rock-solid Magento Development and Deployment Workflows
AOE
 
Front end performance tip
Front end performance tip
Steve Yu
 
WebPagetest Power Users - Velocity 2014
WebPagetest Power Users - Velocity 2014
Patrick Meenan
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
Edureka!
 
#3 Hanoi Magento Meetup - Part 2: Scalable Magento Development With Containers
#3 Hanoi Magento Meetup - Part 2: Scalable Magento Development With Containers
Hanoi MagentoMeetup
 
Single page apps with drupal 7
Single page apps with drupal 7
Chris Tankersley
 
Pyramid Deployment and Maintenance
Pyramid Deployment and Maintenance
Jazkarta, Inc.
 

Similar to Creating a full stack web app with python, npm, webpack and react (20)

WEBPACK
WEBPACK
home
 
How to Webpack your Django!
How to Webpack your Django!
David Gibbons
 
Webpack presentation
Webpack presentation
RAHUL SHARMA
 
Improving build solutions dependency management with webpack
Improving build solutions dependency management with webpack
NodeXperts
 
Nuxtjs cheat-sheet
Nuxtjs cheat-sheet
ValeriaCastillo71
 
Ultimate Survival - React-Native edition
Ultimate Survival - React-Native edition
Richard Radics
 
ReactJS Workflows
ReactJS Workflows
Cem Arguvanlı
 
Introduction of webpack 4
Introduction of webpack 4
Vijay Shukla
 
Modern Web Application Development Workflow - EclipseCon Europe 2014
Modern Web Application Development Workflow - EclipseCon Europe 2014
Stéphane Bégaudeau
 
Magento 2 Development
Magento 2 Development
Duke Dao
 
Zero Downtime Deployment with Ansible
Zero Downtime Deployment with Ansible
Stein Inge Morisbak
 
2019 Chef InSpec Jumpstart Part 2 of 2
2019 Chef InSpec Jumpstart Part 2 of 2
Larry Eichenbaum
 
Modern Web Application Development Workflow - EclipseCon France 2014
Modern Web Application Development Workflow - EclipseCon France 2014
Stéphane Bégaudeau
 
How to install ReactJS software
How to install ReactJS software
VigneshVijay21
 
ReactJS software installation
ReactJS software installation
HopeTutors1
 
Zero Downtime Deployment with Ansible
Zero Downtime Deployment with Ansible
Stein Inge Morisbak
 
Learn react-js
Learn react-js
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
Room with a Vue - Introduction to Vue.js
Room with a Vue - Introduction to Vue.js
Zachary Klein
 
Getting Started with React v16
Getting Started with React v16
Benny Neugebauer
 
Love at first Vue
Love at first Vue
Dalibor Gogic
 
WEBPACK
WEBPACK
home
 
How to Webpack your Django!
How to Webpack your Django!
David Gibbons
 
Webpack presentation
Webpack presentation
RAHUL SHARMA
 
Improving build solutions dependency management with webpack
Improving build solutions dependency management with webpack
NodeXperts
 
Ultimate Survival - React-Native edition
Ultimate Survival - React-Native edition
Richard Radics
 
Introduction of webpack 4
Introduction of webpack 4
Vijay Shukla
 
Modern Web Application Development Workflow - EclipseCon Europe 2014
Modern Web Application Development Workflow - EclipseCon Europe 2014
Stéphane Bégaudeau
 
Magento 2 Development
Magento 2 Development
Duke Dao
 
Zero Downtime Deployment with Ansible
Zero Downtime Deployment with Ansible
Stein Inge Morisbak
 
2019 Chef InSpec Jumpstart Part 2 of 2
2019 Chef InSpec Jumpstart Part 2 of 2
Larry Eichenbaum
 
Modern Web Application Development Workflow - EclipseCon France 2014
Modern Web Application Development Workflow - EclipseCon France 2014
Stéphane Bégaudeau
 
How to install ReactJS software
How to install ReactJS software
VigneshVijay21
 
ReactJS software installation
ReactJS software installation
HopeTutors1
 
Zero Downtime Deployment with Ansible
Zero Downtime Deployment with Ansible
Stein Inge Morisbak
 
Room with a Vue - Introduction to Vue.js
Room with a Vue - Introduction to Vue.js
Zachary Klein
 
Getting Started with React v16
Getting Started with React v16
Benny Neugebauer
 
Ad

Recently uploaded (20)

Advanced Token Development - Decentralized Innovation
Advanced Token Development - Decentralized Innovation
arohisinghas720
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
Rierino Commerce Platform - CMS Solution
Rierino Commerce Platform - CMS Solution
Rierino
 
How Insurance Policy Management Software Streamlines Operations
How Insurance Policy Management Software Streamlines Operations
Insurance Tech Services
 
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Safe Software
 
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Varsha Nayak
 
SAP PM Module Level-IV Training Complete.ppt
SAP PM Module Level-IV Training Complete.ppt
MuhammadShaheryar36
 
Decipher SEO Solutions for your startup needs.
Decipher SEO Solutions for your startup needs.
mathai2
 
Insurance Underwriting Software Enhancing Accuracy and Efficiency
Insurance Underwriting Software Enhancing Accuracy and Efficiency
Insurance Tech Services
 
wAIred_RabobankIgniteSession_12062025.pptx
wAIred_RabobankIgniteSession_12062025.pptx
SimonedeGijt
 
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
How to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdf
Creative Fosters
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
Advanced Token Development - Decentralized Innovation
Advanced Token Development - Decentralized Innovation
arohisinghas720
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
Rierino Commerce Platform - CMS Solution
Rierino Commerce Platform - CMS Solution
Rierino
 
How Insurance Policy Management Software Streamlines Operations
How Insurance Policy Management Software Streamlines Operations
Insurance Tech Services
 
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Safe Software
 
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Varsha Nayak
 
SAP PM Module Level-IV Training Complete.ppt
SAP PM Module Level-IV Training Complete.ppt
MuhammadShaheryar36
 
Decipher SEO Solutions for your startup needs.
Decipher SEO Solutions for your startup needs.
mathai2
 
Insurance Underwriting Software Enhancing Accuracy and Efficiency
Insurance Underwriting Software Enhancing Accuracy and Efficiency
Insurance Tech Services
 
wAIred_RabobankIgniteSession_12062025.pptx
wAIred_RabobankIgniteSession_12062025.pptx
SimonedeGijt
 
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
How to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdf
Creative Fosters
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
Ad

Creating a full stack web app with python, npm, webpack and react

  • 1. CREATING A FULL STACK WEB APP WITH PYTHON, NPM, WEBPACK AND REACT by Angela Branaes
  • 6. THE STRUCTURE OF A WEB APP ➤ Separated into front and back end. ➤ Multiple technologies. ➤ Front end is: ➤ Look and feel. ➤ User interaction. ➤ Back end is: ➤ Long running operations. ➤ Data, and data processing. ➤ Application logic.
  • 7. INITIAL PROJECT SETUP ➤ Create the following directory structure . ├── README.md └── fullstack_template/ ├── server/ └── static/ ├── css/ ├── dist/ ├── images/ └── js/ $ cd fullstack_template/static
  • 8. WE’RE GOING TO NEED A JAVASCRIPT PACKAGE MANAGER ➤ Using a package manager makes it painless to: ➤ Keep your project dependencies up to date. ➤ Fetch and install new packages. ➤ Similar to brew, pip, pacman etc.
  • 9. NPM
  • 10. NPM = NODE PACKAGE MANAGER ➤ Automatically included with Node.js ➤ Easy to use ➤ well documented ➤ nearly 500 000 packages available ➤ Initialise NPM: $ npm init ➤ This creates a file called package.json
  • 11. OUR UPDATED DIRECTORY TREE . ├── README.md └── fullstack_template/ ├── server/ └── static/ ├── css/ ├── dist/ ├── images/ ├── js/ └── package.json
  • 12. THE PURPOSE OF PACKAGE.JSON 1. Keeps track of dependencies and versions. 2. Informs other developers about your project. 3. Makes installing, running and updating a project automatic and reproducible
  • 13. THIS IS MY PACKAGE.JSON { "name": "FullStackTemplate", "version": "1.0.0", "description": "Fullstack Template", "main": "index.jsx", "scripts": { "test": "echo "Error: no test specified" && exit 1", }, "keywords": [ "python", "react", "npm", "webpack" ], "author": "Angela Branaes", "license": "MIT", "devDependencies": { "webpack": "^3.0.0" } }
  • 14. WHY WE NEED A MODULE BUNDLER ➤ Minimises the number of <script> tags in your HTML ➤ This means faster loading, and less hassle. ➤ Don’t need to worry about bundle import ordering! ➤ Don’t have to use globals or namespacing. ➤ Lazy module loading. ➤ We will be using Webpack.
  • 16. WEBPACK ➤ Only understands JavaScript. ➤ Everything else needs a loader or plugin. ➤ Everything is a module. ➤ You can require() JavaScript, React, CSS or images. ➤ Creates a dependency tree of all your required modules, and packages those into bundles. ➤ Easy to get started with, if you only need JavaScript. ➤ Minor learning curve for css/less and image loading. ➤ Lets you split your bundles and load them asynchronously and lazily. ➤ Integrates seamlessly with npm.
  • 17. INSTALL & CONFIGURE WEBPACK ➤ Install webpack: $ npm i webpack --save-dev ➤ Add a file named webpack.config.js: const webpack = require(‘webpack'); const config = { entry: __dirname + '/js/index.jsx', output: { path: __dirname + '/dist', filename: 'bundle.js', }, resolve: { extensions: ['.js', '.jsx', '.css'] }, }; module.exports = config;
  • 18. OUR UPDATED DIRECTORY TREE . ├── README.md └── fullstack_template/ ├── server/ └── static/ ├── css/ ├── dist/ ├── images/ ├── js/ ├── package.json └── webpack.config.js
  • 19. SINGLE VS MULTI PAGE APPLICATIONS ➤ I mostly use Webpack for single page applications. ➤ You can also use webpack for multi page applications: entry: {
 “indexPage”: __dirname + “js/index.jsx”,
 “aboutPage”: __dirname + “js/about.jsx” }, output: { path: __dirname + “/dist”,
 filename: “[name].js” //e.g. aboutPage.js }
  • 21. OUR UPDATED DIRECTORY TREE . ├── README.md └── fullstack_template/ ├── server/ └── static/ ├── css/ ├── dist/ │ └── bundle.js ├── images/ ├── js/ │ └── index.jsx ├── index.html ├── package.json └── webpack.config.js
  • 22. CREATE INDEX.HTML ➤ A really simple index.html is all you need. ➤ The index.html just loads your JavaScript bundle <body> <div id="content" /> <script src="dist/bundle.js" type=“text/javascript”> </script> </body> ➤ All the layout and behaviour live in the JS/React files!
  • 23. CREATE INDEX.JSX ➤ Just 1 line of Plain Old JavaScript alert(“Hello World!”);
  • 24. BUILDING YOUR APPLICATION ➤ Create run commands to simplify building your code. ➤ Run commands are just aliases. ➤ Example from my package.json: “scripts": { "watch": "webpack --progress -d --config webpack.config.js —watch" } ➤ Makes the dev process more fluid. ➤ I always add the following: ➤ build ➤ dev-build ➤ watch
  • 25. START THE WATCH COMMAND ➤ Any changes get built automatically $ npm run watch ➤ Open index.html….
  • 27. CREATING A SIMPLE REACT APP
  • 28. THE BENEFITS OF USING REACT ➤ Easy to build UIs composed of small, distinct components. ➤ Encourages design of reusable UI components. ➤ Easier to understand what’s going on when markup lives with the code (JSX). ➤ Automatic re-rendering of components on change. ➤ Easy to maintain.
  • 29. INSTALL REACT $ npm i react react-dom —save-dev
  • 30. REACTIFY INDEX.JSX import React from "react"; import ReactDOM from "react-dom"; import App from “./App"; ReactDOM.render(<App />, document.getElementById("content"));
  • 31. ADD A REACT “APP” CLASS ➤ Remember to export your react classes! // App.jsx import React from “react”; export default class App extends React.Component { render () { return <p> Hello React!</p>; } }
  • 32. OUR UPDATED DIRECTORY TREE . ├── README.md └── fullstack_template/ ├── server/ └── static/ ├── css/ ├── dist/ ├── images/ ├── js/ │ ├── index.jsx │ └── App.jsx ├── index.html ├── package.json └── webpack.config.js
  • 33. WHAT IS JSX? ➤ Syntax extension to JavaScript. ➤ Optimised on compilation, so faster than JavaScript. ➤ Statically typed and mostly type-safe. JavaScript is not. ➤ Lets you write HTML tags in your JavaScript functions: <Hello name=“Rimini” /> instead of React.createElement(Hello, {name: “Rimini}, null) ➤ Recognises upper-case element-types as written in React. E.g. <Hello /> ➤ How do we make our browser understand JSX?
  • 34. INTRODUCING BABEL Write next-generation javascript right now! Transform JSX to JS.
  • 35. ADD BABEL ➤ Install Babel ➤ Add the Babel presets to the package.json: “babel”: { “presets”: [ “es2015”, “react” ] },
  • 36. ADD A BABEL-LOADER RULE TO THE WEBPACK CONFIG: module: { rules: [ { test: /.jsx?/, exclude: /node_modules/, use: 'babel-loader' } ] }
  • 37. OPEN INDEX.HTML IN YOUR BROWSER ➤ This should now show the “Hello React” paragraph we added in our new React App.jsx file.
  • 40. PYTHON ➤ Go to the server folder ➤ Ensure you’re in a Python virtualenv $ mkvirtualenv fullstack ➤ Install Flask (fullstack)$ pip install flask ➤ Create a file called server.py in the server folder
  • 41. OUR UPDATED DIRECTORY TREE . ├── README.md └── fullstack_template/ ├── server/ │ └── server.py └── static/ ├── css/ ├── dist/ ├── images/ ├── js/ │ ├── index.jsx │ └── App.jsx ├── index.html ├── package.json └── webpack.config.js
  • 42. SERVER.PY from flask import Flask, render_template app = Flask(__name__, static_folder=“../static/dist", template_folder="../static") @app.route("/") def index(): return render_template("index.html") @app.route("/hello") def hello(): return "Hello World!” if __name__ == "__main__": app.run()
  • 43. START THE SERVER ➤ Go to: http://localhost:5000/ $ python server.py
  • 44. REQUEST INFO FROM THE SERVER var $ = require(‘jquery'); getPythonHello() { $.get(window.location.href + 'hello', (data) => { console.log(data); this.personaliseGreeting(data); }); }
  • 45. MAKE PYTHON DO SOMETHING MORE INTERESTING ➤ We call get_hello() whenever we hit the /hello endpoint def get_hello(): greeting_list = ['Ciao', 'Hei', 'Salut', 'Hola', 'Hallo', 'Hej'] return random.choice(greeting_list)
  • 46. SAY HI TO SOMEONE SPECIAL ➤ There should be a class for that! ➤ Change the App.jsx render method to the following: render () { return ( <PageHeader> <div className='header-contents'> <Hello name='Rimini' /> </div> </PageHeader> ); )
  • 47. OUR UPDATED DIRECTORY TREE . ├── README.md └── fullstack_template/ ├── server/ │ └── server.py └── static/ ├── css/ ├── dist/ ├── images/ ├── js/ │ ├── index.jsx │ ├── App.jsx │ └── Hello.jsx ├── index.html ├── package.json └── webpack.config.js
  • 48. CREATE THE HELLO CLASS export default class Hello extends React.Component { constructor(props) { super(props); // greeting is now “Hello Rimini” this.state = {greeting: 'Hello ' + this.props.name}; // This binding is necessary to make `this` // work in the button callback this.getPythonHello = this.getPythonHello.bind(this); } }
  • 49. ➤ Add this function to the Hello class: ➤ This will re-render the greeting on our website to a new one when called. personaliseGreeting(greeting) { this.setState({greeting: greeting + ' ' + this.props.name + '!'}); }
  • 50. LET’S FINALLY RENDER OUR HELLO! render () { return ( <h1>{this.state.greeting}</h1> <hr/> <Button onClick={this.getPythonHello}> Say Hello! </Button> ); }
  • 53. CREATE A NICE LAYOUT WITH CSS ➤ Webpack ONLY understands JavaScript. ➤ Install the following loaders and plugins: ➤ style-loader ➤ css-loader ➤ extract-text-webpack-plugin ➤ Add a plugin to the webpack config: plugins: [ new ExtractTextPlugin('styles.css'), ]
  • 54. OUR UPDATED DIRECTORY TREE . ├── README.md └── fullstack_template/ ├── server/ │ └── server.py └── static/ ├── css/ │ └── fullstack.css ├── dist/ │ ├── bundle.js │ └── styles.css ├── images/ ├── js/ │ ├── index.jsx │ ├── App.jsx │ └── Hello.jsx ├── index.html ├── package.json └── webpack.config.js
  • 55. ➤ Add fullstack.css to the css folder ➤ Add a few css rules for our header ➤ require fullstack.css in App.jsx: require(‘../css/fullstack.css'); ➤ Add the bundled css to the index.html: <link rel="stylesheet" href="dist/styles.css">
  • 57. OUR UPDATED DIRECTORY TREE . ├── README.md └── fullstack_template/ ├── server/ │ └── server.py └── static/ ├── css/ │ └── fullstack.css ├── dist/ │ ├── bundle.js │ └── styles.css ├── images/ │ └── header.png ├── js/ │ ├── index.jsx │ ├── App.jsx │ └── Hello.jsx ├── index.html ├── package.json └── webpack.config.js
  • 58. ADD A NICE BACKGROUND IMAGE 1. Add the file-loader loader 2. Add an image to your images/ folder 3. Set the image to be your header background in the fullstack.css file background-image: url(‘../images/header.jpg’); 4. NOTE: you have to load the image in your React app for it to show up!
  • 59. IN APP.JSX import HeaderBackgroundImage from ‘../images/header.jpg’; ➤ Add this fn to your class: addHeaderImg() { let headerBg = new Image(); headerBg.src = HeaderBackgroundImage; } ➤ And this to your render(): {this.addHeaderImg()}
  • 64. THANK YOU! ANY QUESTIONS? @angineering @angineering - The code is on GitHub: https://github.com/angineering/FullStackTemplate @angineering - This talk is also a blogpost