Enzyme

Enzyme is a Javascript testing utility for React. In the below tutorial I will show you how to get it working with your react classes.

Below you begin be sure to following the tutorial on Flask: React Website.

*** If you don’t have a .babelrc file setup and configured then it won’t work. This is required.

Folder Structure:
  • Inside testApp create the following:
    • folder: tests
      • folder: home
        • file: home.js
      • file: dom.js
      • file: helpers.js
Install NPM Modules:
  1. npm install enzyme --save-dev
  2. npm install enzyme-adapter-react-16 --save-dev
  3. npm install ignore-styles --save-dev
  4. npm install chai --save-dev
  5. npm install jsdom --save-dev
  6. npm install mocha --save-dev
  7. npm install sinon --save-dev
Package.json:

Under “scripts” add the “test” run command.

  1. "test": "mocha --require babel-core/register --require ignore-styles --require ./tests/helpers.js --require ./tests/dom.js --recursive \"./tests/**/*.js*\""
dom.js:

This will setup all the dom for enzyme.

  1. var jsdom = require("jsdom");
  2. const { JSDOM } = jsdom;
  3. var exposedProperties = ["window", "navigator", "document"];
  4. const { document } = (new JSDOM("<html><head></head><body><div class='container-body' id='app'></div></body></html>")).window;
  5. global.document = document;
  6. global.window = document.defaultView;
  7.  
  8. Object.keys(document.defaultView).forEach((property) => {
  9. if (typeof global[property] === "undefined") {
  10. exposedProperties.push(property);
  11. global[property] = document.defaultView[property];
  12. }
  13. });
  14.  
  15. Object.keys(window).forEach((key) => {
  16. if (!(key in global)) {
  17. global[key] = window[key];
  18. }
  19. });
  20.  
  21. //This is so jquery is available if using $
  22. global.$ = require("jquery");
  23. //This is so jquery is available if using jQuery
  24. global.jQuery = require("jquery");
  25. //This is so L is available if using leaflet otherwise don't include
  26. global.L = require("leaflet");
  27. global.navigator = global.window.navigator;
helpers.js:

This just set’s up enzyme configuration and allows sinon, etc through to each javascript test file.

  1. import { expect } from "chai";
  2. import { sinon, spy } from "sinon";
  3.  
  4. var enzyme = require("enzyme");
  5. var Adapter = require("enzyme-adapter-react-16");
  6.  
  7. enzyme.configure({ adapter: new Adapter() });
  8.  
  9. global.expect = expect;
  10. global.sinon = sinon;
  11. global.spy = spy;
  12. global.mount = enzyme.mount;
  13. global.render = enzyme.render;
  14. global.shallow = enzyme.shallow;
home.js:

*** it should be mentioned that the component must be exported. If your file has ReactDOM.render in it then nothing will work correctly. Must be an exported component. So if you followed the tutorial mentioned above then you will need to adjust home.jsx file to mimic this.

  1. import React from "react";
  2. import {Home} from "../../app/home/js/homeComponent.jsx";
  3.  
  4. describe('HomeComponent', () => {
  5. it('Tests that componentdidmount got called', () => {
  6. global.spy(Home.prototype, 'componentDidMount');
  7. const wrapper = mount(<Home />);
  8. expect(Home.prototype.componentDidMount.calledOnce).to.equal(true);
  9. });
  10. });

React, Babel + Eslint Configuration

If you want to configure your npm package to have JavaScript static code analysis then you will need to configure some form of js linter. For the purposes of this demo I will use eslint.

Before We Begin:

Packages:

Package Installations:

  1. npm install --save-dev babel
  2. npm install --save-dev babel-core
  3. npm install --save-dev babel-loader
  4. npm install --save-dev babel-preset-es2015
  5. npm install --save-dev babel-eslint //OPTIONAL
  6. npm install --save-dev eslint
  7. npm install --save-dev eslint-loader
  8. npm install --save-dev eslint-plugin-react
  9. npm install --save-dev uglifyjs-webpack-plugin

Webpack.config.js:

Webpack 3 Plugins

Add “NoEmitOnErrorsPlugin” to ensure that emitting doesn’t occur if errors are encountered.

Add “UglifyJsPlugin” to minimize your js

Add “AggressiveMergingPlugin” to get more aggressive chunk merging.

Don’t forget the loaders for babel and eslint.

  1. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  2.  
  3. #Under module.exports add
  4. plugins: [
  5. new webpack.NoEmitOnErrorsPlugin(),
  6. new UglifyJsPlugin(),
  7. new webpack.optimize.AggressiveMergingPlugin()
  8. ],
  9. module: {
  10. loaders: [
  11. {
  12. enforce: "pre",
  13. test: /\.jsx?$/,
  14. exclude: /(node_modules|thirdparty)/,
  15. loader: "eslint-loader",
  16. },
  17. {
  18. test: /\.jsx?$/,
  19. exclude: /(node_modules)/,
  20. loader: "babel-loader",
  21. query: {
  22. presets: ["es2015","react"],
  23. }
  24. },
  25. ]
  26. }
  27.  
  28.  

.eslintrc

You can set anything in your .eslintrc file. Here are just some that I use. All the rules starting with “react” are from “eslint-plugin-react” package.

  1. {
  2. "parser": "babel-eslint",
  3. "env": {
  4. "browser": true,
  5. "mocha": true,
  6. "es6": true
  7. },
  8. "globals": {
  9. "require": true
  10. },
  11. "plugins": [
  12. "react"
  13. ],
  14. "rules": {
  15. "new-cap": 0, //disabled
  16. "strict": 0, //disabled
  17. "semi": [2, "always"], //semi-colon required
  18. "no-underscore-dangle": 0, //no underscores before and after off
  19. "no-use-before-define": 1, //Make sure you define then use
  20. "eol-last": 0, //File doesn't need a newline at end
  21. "no-trailing-spaces": [2, { skipBlankLines: true }],
  22. "no-unused-vars": [1, {"vars": "all", "args": "none"}],
  23. "quotes": [
  24. 2,
  25. "double"
  26. ],
  27.   "jsx-quotes": [2, "prefer-double"], //Must use double quotes
  28. "react/jsx-boolean-value": 1, //warning when no value is set for boolean
  29. "react/jsx-no-undef": 2, //error: undeclared variables
  30. "react/jsx-uses-react": 1, //Warn: Prevent incorrectly unused
  31. "react/jsx-uses-vars": 1, //Warn: Prevent unused variables
  32. "react/no-did-mount-set-state": 0, //Off: Prevent setState in componentDidMount
  33. "react/no-did-update-set-state": 0, //Off: Prevent setState in componentDidUpdate
  34. "react/no-multi-comp": 1, //Warn: Prevent only one component per file
  35. "react/no-unknown-property": 1, //Warn: Prevent unknown DOM
  36. "react/react-in-jsx-scope": 1, //Warn: Prevent missing React
  37. "react/self-closing-comp": 1 //Warn: Prevent extra closing tags
  38. }
  39. }

.eslintignore

Use this file to ignore any files or folders.

  1. app/folder/**

.babelrc

This is the config file for babel.

  1. {
  2. "presets": [ "es2015" ]
  3. }

Package.json

If you want to run eslint from npm then you have to modify your package.json with the following line. –ext is what extension you want to test against.

If you want a third party to run reports on your eslint then add to the end “-f checkstyle > eslint.xml

  1. "scripts": {
  2. "eslint": "eslint app --ext .jsx --ext .js"
  3. }

Django: React Website

In this tutorial I will demonstrate how to create a Django + React website using Django 2.0. You must have Eclipse installed before you continue. If you have it already installed and configured you can continue on. We will require Postgres 9.4, nodejs before you continue. You can get Nodejs from here. You can get Postgres 9.4 from here.

Pip Django Install:
  1. pip install django
  2. pip install django-webpack-loader
Django Version:

If you are not sure what version you are running do the following

  1. python -c "import django; print(django.get_version())"
Eclipse Create Project:

 

 

 

 

 

 

Eclipse Setup Project:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Eclipse Django DB Settings:

 

 

 

 

 

 

 

 

 

 

 

 

 

Eclipse Django Setup Successful:

Once you click “Finish” your project will look like the following.

 

 

 

Folder Structure:
  • Under djangoApp project.
  • folder: static
  • folder: djangoApp
    • folder: templates
      • file: index.html
      • folder: base
        • file: base.html
  • folder: assets
    • folder: bundles
    • folder: js
      • file: index.jsx
Node:

Inside the djangoApp application do the following

  1. npm init
  2. npm install --save-dev jquery react react-dom webpack webpack-bundle-tracker babel-loader babel-core babel-preset-es2015 babel-preset-react
  3. npm install create-react-class --save
webpack.config.js:
  1. var path = require('path')
  2. var webpack = require('webpack')
  3. var BundleTracker = require('webpack-bundle-tracker')
  4.  
  5. module.exports = {
  6. //the base directory (absolute path) for resolving the entry option
  7. context: __dirname,
  8. //the entry point we created earlier. Note that './' means
  9. //your current directory.
  10. entry: {
  11. "index": [path.resolve(__dirname, "./assets/js/index.jsx")],
  12. },
  13. output: {
  14. path: path.resolve('./assets/bundles/'),
  15. filename: "[name]-[hash].js",
  16. },
  17. plugins: [
  18. //tells webpack where to store data about your bundles.
  19. new BundleTracker({filename: './webpack-stats.json'}),
  20. //makes jQuery available in every module
  21. new webpack.ProvidePlugin({
  22. $: 'jquery',
  23. jQuery: 'jquery',
  24. 'window.jQuery': 'jquery'
  25. })
  26. ],
  27. module: {
  28. loaders: [
  29. {
  30. test: /\.jsx?$/,
  31. exclude: /(node_modules)/,
  32. loader: 'babel-loader',
  33. query: {
  34. presets: ['react','es2015']
  35. }
  36. }
  37. ]
  38. }
  39. }
djangoApp\Settings.py:

Installed Apps

  1. INSTALLED_APPS = [
  2. 'django.contrib.admin',
  3. 'django.contrib.auth',
  4. 'django.contrib.contenttypes',
  5. 'django.contrib.sessions',
  6. 'django.contrib.messages',
  7. 'django.contrib.staticfiles',
  8. 'webpack_loader',
  9. ]

Add/Edit the following template directive

  1. TEMPLATES = [
  2. {
  3. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  4. 'DIRS': [os.path.join(BASE_DIR, 'djangoApp', 'templates'),],
  5. 'APP_DIRS': True,
  6. 'OPTIONS': {
  7. 'context_processors': [
  8. 'django.template.context_processors.debug',
  9. 'django.template.context_processors.request',
  10. 'django.contrib.auth.context_processors.auth',
  11. 'django.contrib.messages.context_processors.messages',
  12. ],
  13. },
  14. },]

Add the following static directive

  1. STATIC_URL = '/static/'
  2.  
  3. STATICFILES_DIRS = [
  4. os.path.join(BASE_DIR, 'assets'),
  5. ]

Modify DATABASES

  1. DATABASES = {
  2. 'default': {
  3. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  4. 'NAME': 'YOUR_DB_NAME',
  5. 'USER': 'YOUR_USER',
  6. 'PASSWORD': 'YOUR_PASSWORD',
  7. 'HOST': 'localhost',
  8. 'PORT': 5432
  9. }
  10. }

Webpack Loader

  1. WEBPACK_LOADER = {
  2. 'DEFAULT': {
  3. 'BUNDLE_DIR_NAME': 'bundles/',
  4. 'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
  5. }
  6. }
djangoApp\views.py:

We will create our index page view. Notice the third dict. Those are variables passed to the template to make our site dynamic

  1. from django.shortcuts import render
  2.  
  3. def index(request):
  4. return render(request, 'index.html', {'title': 'Index Page', 'script_name': 'index'})
djangoApp\urls.py:

Add the following imports

  1. from django.conf.urls import url
  2. #This is the index view we created above
  3. from djangoApp.views import index
  4.  
  5. urlpatterns = [
  6. url(r'^$', index, name='index'),
  7. path('admin/', admin.site.urls),
  8. ]
djangoApp\templates\base\base.html:

Let’s setup our base template and setup our blocks that the other templates will inherit from.

  1. <html>
  2. <head>
  3. <title>{% block title %}{% endblock %}</title>
  4. </head>
  5. <body>
  6. {% block content %}
  7. {% endblock %}
  8. </body>
  9. </html>
djangoApp\templates\index.html:

The important parts here are the extends otherwise your base.html template won’t be inherited. As well the {% with %} and title variable makes our template dynamic and allows us to incorporate react in our site.

  1. {% extends "base/base.html" %}
  2. {% load render_bundle from webpack_loader %}
  3. {% load staticfiles %}
  4. {% block title %}
  5. {{title}}
  6. {% endblock %}
  7. {% block content %}
  8. <div id="container"></div>
  9. {% with script=script_name %}
  10. {% render_bundle script 'js' %}
  11. {% endwith %}
  12. {% endblock %}
assets\js\index.jsx:

This is our react class.

  1. var React = require('react');
  2. var ReactDOM = require('react-dom');
  3. var createReactClass = require('create-react-class');
  4.  
  5. var App = createReactClass({
  6. render: function() {
  7. return (
  8. <h1>
  9. React App Page
  10. </h1>
  11. )
  12. }
  13. });
  14.  
  15. ReactDOM.render(<App />, document.getElementById('container'));
Database Setup/Migration:

For this tutorial we used postgres. At this time please make sure you create your djangoApp db and user you specified in the settings.py file. Then run the following commands in order.

  1. #Migrates the auth
  2. python manage.py migrate auth
  3. #migrates the rest
  4. python manage.py migrate
  5. #Create the user for accessing the django admin ui
  6. #This will ask you for user names and passwords. Don't make it the same as in your settings.py file.
  7. python manage.py createsuperuser
Start Server:
  1. webpack -p
  2. python manage.py runserver

Your site is now running at http://localhost:8000.

Your admin site is now running at http://localhost:8000/admin/.

 

References:

I used this video as a guideline to get the project started. However some didn’t work right and needed to adjust and made adjustments to require just one template, etc.

React: Leaflet Markers

This entry is part 9 of 9 in the series React: Leaflet

In this tutorial I will demonstrate how to add a marker to the map. Refer to the documentation for more information.

Before We Begin:

Text:

LayerGroup onAdd Method

  1. //Define the marker. Since this is text based their is no icon but instead a divIcon
  2. this.textMarker = L.marker([20.5, -0.09],{
  3. icon: L.divIcon({
  4. iconSize: [100,16],
  5. iconAnchor: [22, 30],
  6. className: "",
  7. }),
  8. zIndexOffset: 75,
  9. });
  10.  
  11. //Sets the text
  12. this.textMarker.options.icon.options.html = "This is my text";
  13.  
  14. //Adds the text marker to the layer
  15. this.addLayer(this.textMarker);

LayerGroup onRemove Method

  1. //Remove the text marker you just added
  2. this.removeLayer(this.textMarker);

Results: Now you will see as you turn the layer on and off from the context menu the text will show or hide.

Icon:

Import Image:

  1. import myImagefrom "../images/myImage.png";

Create Icon/Marker onAdd Method

  1. //Define the icon you will be using
  2. var myIcon = L.icon({
  3. iconUrl: myImage,
  4. iconSize: [75, 75], // size of the icon
  5. iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
  6. });
  7.  
  8. //Define the icon based marker
  9. this.marker = L.marker([20.5, -40.09],{
  10. icon: myIcon,
  11. opacity: 0.7,
  12. zIndexOffset: 30
  13. });
  14.  
  15. //Adds the icon marker to the layer
  16. this.addLayer(this.marker);

onRemove Method

  1. //Remove the marker you just added
  2. this.removeLayer(this.marker);

Results: Now you will see as you turn the layer on and off from the context menu the icon will show or hide.

Set Latitude/Longitude:

You can set the latitude and longitude in one of two ways.

  1. //Set the latitude and longitude
  2.  
  3. //Method 1:
  4. L.marker([20.5, -0.09])
  5.  
  6. //Method 2:
  7. this.textMarker.setLatLng([20.5, -0.09]);

Event(s):

onClick

  1. marker.on("click", function(e) {
  2. var marker = e.target;
  3. //Do my work here
  4. }.bind(this));

MouseOver

  1. marker.on("mouseover", function(e) {
  2. var marker = e.target;
  3. //Do my work here
  4. }.bind(this));

MouseOut

  1. marker.on("mouseout", function(e) {
  2. var marker = e.target;
  3. //Do my work here
  4. }.bind(this));

Popup:

BindPopup

  1. marker.bindPopup(L.popup(),{
  2. offset: L.point(0,-10) //You can add an offset if you want to
  3. });

OpenPopup

Let’s say you want to open the popup during a mouseover event.

  1. marker.on("mouseover", function(e) {
  2. var marker = e.target;
  3.  
  4. //Set the content to display in the popup
  5. marker.setPopupContent("My Text");
  6. //Now open the popup
  7. marker.openPopup();
  8. }.bind(this));

ClosePopup

Let’s say you want to open the popup during a mouseout event.

  1. marker.on("mouseout", function(e) {
  2. var marker = e.target;
  3. //Close the popup now
  4. marker.closePopup();
  5. }.bind(this));

React: Leaflet Icon

This entry is part 8 of 9 in the series React: Leaflet

In this tutorial I will demonstrate how to add an icon to the map. Refer to the documentation for more information.

Before We Begin:

Create Folders/Files:

  • app
    • folder: leaflet
      • folder: images
        • file: yourimage.png

Import Image(s):

  1. import yourimage from "../images/yourimage.png";

Create Icon:

  1. var myIcon = L.icon({
  2. iconUrl: plusimg,
  3. iconSize: [75, 75], // size of the icon
  4. iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
  5. });

Add to Map:

[51.5, -0.09] refers to the position on the map you want to add it to. Refer to the marker tutorial for further information on markers.

  1. L.marker([51.5, -0.09], {icon: myIcon}).addTo(this.map);

React: Leaflet Control

This entry is part 7 of 9 in the series React: Leaflet

In this tutorial I will demonstrate how to add a control to the map. Refer to the documentation for more information.

Before We Begin:

LayerControl

You can now do anything you want to do here.

  1. var MyControl = L.Control.extend({
  2. onAdd: function (map){
  3. //Reference to the map
  4. this._map = map;
  5.  
  6. //This is the container to return so it is available on the map
  7. var container = L.DomUtil.create("div");
  8. return container;
  9. },
  10. onRemove: function(){
  11. //Removes the control
  12. L.Control.prototype.onRemove.call(this);
  13. this._map = null;
  14. }
  15. });

Add Control to Map

The options you add in the class definition are the options that the control gets.

  1. var myControl = new MyControl({ position: "bottomright"});
  2. myControl.addTo(this.map);

React: Leaflet LayerGroup

This entry is part 6 of 9 in the series React: Leaflet

In this tutorial I will demonstrate how to add a layer group control. Refer to the documentation for more information.

Before We Begin:

LayerGroup

You can now do anything you want to do here.

  1. var MyLayerGroup = L.LayerGroup.extend({
  2. onAdd: function(map) {
  3. //Reference to the map
  4. this._map = map;
  5. },
  6. onRemove: function(){
  7. //Removes the layer
  8. L.LayerGroup.prototype.onRemove.call(this, map);
  9. },
  10. initialize: function (options) {
  11. L.LayerGroup.prototype.initialize.call(this);
  12. //The options sent in from initialisation
  13. L.Util.setOptions(this, options);
  14. }
  15. });

Add LayerGroup to Map

  1. var myLayerGroup = new MyLayerGroup();
  2. myLayerGroup.addTo(this.map);

Overlay

If you want to add the layergroup to overlays menu.

  1. this.layerControl.addOverlay(myLayerGroup, "My Layer Group");

React: Leaflet DomEvent

This entry is part 5 of 9 in the series React: Leaflet

In this tutorial I will demonstrate how to add events to your html control. Refer to the documentation for more information.

Before We Begin:

OnClick

  1. L.DomEvent.on(htmlControl, "click", (e) => {
  2. //Do your work here
  3. });

 

 

 

React: Leaflet html Controls

This entry is part 4 of 9 in the series React: Leaflet

In this tutorial I will demonstrate how to add html controls to your leaflet map. Refer to the documentation for more information.

Before We Begin:

Really you can create any html control just substitute div for whatever you want. Below are just some basic examples.

Div

  1. var divContainer = L.DomUtil.create("div", "myClassName");

Image

  1. var img = L.DomUtil.create("img", "myClassName");

Label

  1. var label = L.DomUtil.create("label", "myClassName");

Span

  1. var span = L.DomUtil.create("span", "myClassName");

Input

  1. var input = L.DomUtil.create("input", "myClassName");

BR

  1. var br = L.DomUtil.create("br", "myClassName");

You can modify the html control with additional values such as

  1. #style
  2. divContainer.style.display = "";
  3.  
  4. #id
  5. divContainer.id = "myId";
  6.  
  7. #name
  8. divContainer.name = "myId";
  9.  
  10. #img src
  11. divContainer.src = "";
  12.  
  13. #title
  14. divContainer.title = "";
  15.  
  16. #innerHTML
  17. divContainer.innerHTML = "";
  18.  
  19. #type
  20. divContainer.type= "checkbox";
  21.  
  22. #checked
  23. divContainer.checked= false;
  24.  
  25.  

Parent Control

You can add the control to a parent control in the following way.

  1. var divContainer = L.DomUtil.create("div", "myClassName");
  2.  
  3. var subContainer = L.DomUtil.create("div", "myClassName", divContainer);

React: Leaflet EasyButton

This entry is part 3 of 9 in the series React: Leaflet

In this tutorial I will demonstrate how to add a easy button to your leaflet map. This will just be a basic example. For more information refer to the documentation.

Before We Begin:

Node Package Install:

  1. npm install leaflet-easybutton --save

Edit app/home/leaflet/js/map.jsx:

  1. //Add the leaflet easybutton package
  2. require("leaflet-easybutton");
  3. require("leaflet-easybutton/src/easy-button.css");
  4.  
  5. //Somehwere on your page add the following.
  6.  
  7.  
  8. //Check out https://github.com/CliffCloud/Leaflet.EasyButton for more uses
  9. L.easyButton("<div id="tag" class="glyphicon glyphicon-tag" />", function(btn, map) {
  10. map.setView([42.3748204,-71.1161913],16);
  11. }, { position: "topleft"}).addTo(this.map);
  12.  

React: Leaflet Modal

This entry is part 2 of 9 in the series React: Leaflet

In this tutorial I will demonstrate how to add a modal dialog to your leaflet map. Refer to the documentation for more information.

Before We Begin:

Node Package Install:

  1. npm install leaflet-modal --save

Edit app/home/leaflet/js/map.jsx:

  1. //Add the leaflet modal package
  2. require("leaflet-modal");
  3. require("leaflet-modal/dist/leaflet.modal.min.css");
  4.  
  5. //Somewhere on your page add the following. You can even put it on a onclick event or whatever
  6.  
  7. //Create a test control for your modal control.
  8. this.modalContainer = L.DomUtil.create("div");
  9. map.openModal({
  10. zIndex: 10000,
  11. element: this.modalContainer, //The container to display in the modal
  12. OVERLAY_CLS: "overlay", //This is a built in class which you can change if you want
  13. MODAL_CLS: "modal", //This is a built in class which you can change if you want
  14. height: 200, width: 200,
  15. MODAL_CONTENT_CLS: "modal-content", //This is a built in class which you can change if you want
  16. CLOSE_CLS: "modal-close-hide" //The property for close class
  17. });

Edit app/home/leaflet/css/map.css:

  1. .modal-close-hide {
  2. /*Class for hiding the modal*/
  3. display: none;
  4. }

React: Basic Leaflet Map Example

This entry is part 1 of 9 in the series React: Leaflet

In this tutorial I will walk you through incorporating leaflet map in your application. This is just a basic setup walkthrough. We do not deal with overlays or extending controls.

Documentation:

Leaflet
Leaflet Providers

Before We Begin:

If you have not done so already refer to how to Build a React/Python Site to get your basic site up and running.

Node Package Installs:

  1. npm install leaflet-providers --save
  2. npm install leaflet --save

Create Needed Folders/Files:

  • app
    • folder: leaflet
      • folder: css
        • file: map.css
      • folder: js
        • file: map.jsx

Webpack:

Add the following loader for handling images to your webpack.config.js file.

  1. { test: /.*\.(gif|png|jpe?g|svg)$/i, loader: "file-loader?hash=sha512&digest=hex&name=[name].[ext]" }

Edit app/leaflet/js/map.jsx:

We are going to create the map control. Refer to comments below for explanations.

  1. window.jQuery = window.$ = require("jquery");
  2. //React packages
  3. var React = require("react");
  4. var createReactClass = require("create-react-class");
  5.  
  6. //Leaflet packages
  7. import L from "leaflet";
  8. import "leaflet/dist/leaflet.css";
  9.  
  10. //LeafLet providers
  11. require("leaflet-providers");
  12.  
  13. //map css you will create
  14. import "../css/map.css";
  15.  
  16. var LeafLetMap = createReactClass({
  17. getInitialState: function() {
  18. return {};
  19. },
  20. displayMap: function() {
  21. //Setup/initialize the map control
  22. window.map = this.map = L.map("map", {
  23. attributionControl: false, //http://leafletjs.com/reference-1.2.0.html#control-attribution
  24. }).setView([0,0], 1);
  25. //A simple scale control that shows the scale of the current center of screen in metric (m/km) and imperial (mi/ft)
  26. L.control.scale({ //http://leafletjs.com/reference-1.2.0.html#control-scale
  27. metric: false,
  28. imperial: true
  29. }).addTo(this.map);
  30. //You can check out all the free providers here https://github.com/leaflet-extras/leaflet-providers
  31. //Below are just examples
  32. this.layers = {
  33. OpenMapSurferRoads: L.tileLayer.provider('OpenMapSurfer.Roads'),
  34. HyddaFull: L.tileLayer.provider('Hydda.Full'),
  35. StamenTerrain: L.tileLayer.provider('Stamen.Terrain'),
  36. EsriWorldStreetMap: L.tileLayer.provider('Esri.WorldStreetMap'),
  37. EsriWorldTopoMap: L.tileLayer.provider('Esri.WorldTopoMap'),
  38. EsriWorldTerrain: L.tileLayer.provider('Esri.WorldTerrain'),
  39. OpenTopoMap: L.tileLayer.provider('OpenTopoMap'),
  40. OpenStreetMapBlackAndWhite: L.tileLayer.provider('OpenStreetMap.BlackAndWhite'),
  41. OpenStreetMapHOT: L.tileLayer.provider('OpenStreetMap.HOT')
  42. };
  43.  
  44. //Add the default layer you want to use
  45. this.layers.OpenMapSurferRoads.addTo(this.map);
  46.  
  47. //Add the layer control to the top left corner
  48. this.layerControl = L.control.layers(this.layers, {}, { position: "topleft" }).addTo(map);
  49. },
  50. componentDidMount: function() {
  51. //Done after mounting so that it sees the div you are going to map to
  52. this.displayMap();
  53. },
  54. render: function() {
  55. return (
  56. <div className="div-flex">
  57. <div id="map" key="map" className="map"></div>
  58. </div>
  59. );
  60. }
  61. });
  62.  
  63. module.exports = { LeafLetMap:LeafLetMap };

Edit app/leaflet/css/map.css:

  1. .map {
  2. /*Changes the width and height of the map*/
  3. height: 400px;
  4. width: 800px;
  5. }
  6.  
  7. div.leaflet-top {
  8. /*OPTIONAL: This is only needed if your controls start going behind the map.*/
  9. z-index: 1001;
  10. }
  11.  
  12. .leaflet-control-layers-toggle {
  13. /*Changes the width and height of the layer control*/
  14. height: 30px !important;
  15. width: 30px !important;
  16. }

Edit app/home/js/home.jsx:

  1. //Import your leaflet map control from the page you created above
  2. import { LeafLetMap } from "../../leaflet/js/map.jsx";
  3.  
  4. //In the render function add your control
  5. render: function() {
  6. return (
  7. <div className="div-flex">
  8. <LeafLetMap ref="map" />
  9. </div>
  10. );
  11. }

 

 

 

Javascript: Map

You can use the map function to return different results from an array to return a new array with new data.

For example if you want to build an array of controls you could do the following.

  1. var newControls = myDataArray.map(function(rec, index){
  2. return <div></div>;
  3. });

 

JavaScript: Download Object Data

Sometimes you just want to send an object to be downloaded in which you don’t have any special requirements just download the data. To do so is really straight forward. Create the blob with the type then create the blobs object url.

  1. var blob = new Blob([JSON.stringify(myDataObject)], {type: "application/force-download"});
  2. url = URL.createObjectURL(blob);
  3.  
  4. <a target="_blank" href={url} download="file.extension" />

Javascript: Math Functions

In this post I will show you how to perform math operations such as min, max, etc.

Min:

  1. //Array
  2. var maxValue = Math.min.apply(Math, myArray);
  3.  
  4. //Object Array
  5. Math.min.apply(Math,myObjectArray.map(function(v){return v,key;}));

Max:

  1. //Array
  2. var maxValue = Math.max.apply(Math, myArray);
  3.  
  4. //Object Array
  5. Math.max.apply(Math,myObjectArray.map(function(v){return v,key;}));

Sum:

  1. var sumValue = myArray.reduce(function(a, b) { return a + b; }, 0);

Average:

  1. var avgValue = sumValue / myArray.length;

Standard Deviation:

  1. var stdevValue = Math.sqrt(sumValue);

Python: Flask Resource

This tutorial helps setup flask restful api’s.

Install Python Packages:

Open cmd and navigate into your testApp folder and run the following commands.

  1. pip install flask-RESTful && pip freeze > requirements.txt
__init__.py:

On the init of your application you will need to setup flask_restful. There are config options you could set for config.py. Look into it!

  1. from flask_restful import Api
  2. api = Api(app)
  3.  
  4. #Add api endpoints
  5. #Get
  6. api.add_resource(home.views.MyResource, '/home/getMyData/')
  7.  
  8. #Post
  9. api.add_resource(home.views.MyResource, '/home/getMyData/', methods=['POST'])
Setup home views.py:

You need to import Resource in it’s most simplistic form. However if you want to deal with request parameters add in reqparse and inputs. Inputs give you access to boolean that way a boolean can be parsed into a python boolean easily.

  1. from flask_restful import Resource, reqparse, inputs

You can now use get, post, etc. I will give you three examples below.

Get:

  1. class MyResource(Resource):
  2. def get(self):
  3. return {}

Get /w Parameter:

  1. class MyResource(Resource):
  2. def get(self, var):
  3. return {}

Get /w Parameter & Request Parameter:

  1. class MyResource(Resource):
  2. def get(self, var):
  3. parser = reqparse.RequestParser()
  4. parser.add_argument('new_request_var', type=str, default='')
  5.  
  6. #If you want to have a boolean request parameter do the following.
  7. parser.add_argument('new_request_var_bool', type=inputs.boolean, default=False)
  8.  
  9. args = parser.parse_args(strict=True)
  10. new_request_var = args['new_request_var']
  11. new_request_var_bool = args['new_request_var_bool']
  12.  
  13. return {}

Post:

  1. class MyResource(Resource):
  2. def post(self):
  3. return {}

JavaScript: Node & Lodash

In this tutorial we will be giving out some quick examples of Lodash functionality. To know more go here. There are so many examples and ways of doing Lodash. Check out the documentation.

Q: What is Lodash you ask?
A: It is a toolkit of Javascript functions that provides clean, performant methods for manipulating objects and collections. It is a “fork” of the Underscore library and provides additional functionality as well as some serious performance improvements.

First thing we need to do is install it. You will need a node site already ready to go. If you don’t have one you can follow this tutorial on setting a basic one up.

  1. npm install lodash --save

On whatever page you are working with all you need to do is add the following to where your requires are.

  1. var _ = require('lodash');

Now we can use the functionality as we wish. I will do some basic uses below.

Array difference:

If we want to find the difference of an array to the second array. The result would be “1” because 1 is not in the second array. Notice how it does not compare the second array to the first. It’s only checking which values 2 or 1 don’t exist in the second array.

  1. _.difference([2, 1], [2, 3])
Array uniqWith:

If you want to get the unique items in an array you could use the following. It would return “2 45 3 7 8 1” only notice that the additional 45 is not displayed. It has been removed.

  1. _.uniqWith([2, 45, 3, 7, 8, 45, 1], __.isEqual)