How to show in a template the number of online visitors with Django ?

An example of solution to display in a template the number of online visitors in a django web site: [TOC] ### Create a table Visitor_Infos First, create a table in models.py called for example: Visitor_Infos: class Visitor_Infos(models.Model): ip_address = models.GenericIPAddressField() page_visited = models.TextField() event_date = models.DateTimeField(default=datetime.now) ### Edit/create the file processor.py Edit the context file processor.py: from django.conf

How to create a 2d histogram with matplotlib ?

Examples of how to create a 2d histogram with matplotlib [TOC] ### Using the matplotlib hist2d function To create a 2d histogram in python there are several solutions: for example there is the matplotlib function [hist2d](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist2d.html). from numpy import c_ import numpy as np import matplotlib.pyplot as plt import random n = 100000 x = np.random.standard_normal(n) y = 3.0 * x + 2.0 * np.random.standard_normal(n)

Comment afficher dans un template le nombre de visiteurs en ligne sous Django ?

Exemple de solution pour afficher dans un template le nombre de visiteurs en ligne pour un site développé avec Django [TOC] ### Sauvegarder les visiteurs dans une table On peut tout d'abord dans le fichier models.py ajouter la table suivante: class Visitor_Infos(models.Model): ip_address = models.GenericIPAddressField() page_visited = models.TextField() event_date = models.DateTimeField(default=datetime.now) ### Editer le fichier processor.py On peut ensuite éditer le

How to create a json file with the names and attributes of a VIIRS L2 CLDPROP netcdf file using python 3 ?

Example of python 3 code to save in a json file the names and attributes of a VIIRS L2 CLDPROP netcdf file. [TOC] Note: the json file created can be found [here](/Files/viirs-cldprop-l2-001-names-attributes/) ### Read the file import netCDF4 import numpy as np import json import pprint f = netCDF4.Dataset('CLDPROP_L2_VIIRS_SNPP.A2018046.1436.001.2019068074007.nc') ### Get all group names group_dic = f.groups.keys() ### Get names and attributes in each group

How to download a VIIRS L2 CLDPROP netcdf file from ladsweb using python ?

Example of python script to download on your local machine a VIIRS L2 CLDPROP netcdf granule file from [ladsweb](https://ladsweb.modaps.eosdis.nasa.gov/search/) using python. [TOC] Lets consider the following data for example: year = 2018 month = 2 day = 15 hour = 14 minute = 36 Note: VIIRS granules are every 6 minutes ### Convert day to count of days First step we need to get the count of days: from datetime import date d0 = date(year, 1, 1) d1 = date(year

How to get visitor ip address for a django project ?

Example of how to get visitor ip address for a django project: [TOC] ### Get visitor ip address def visitor_ip_address(request): x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') return ip ### Check if ip address is valid To check if the ip address is valid, a solution is to use the [socket](h

How to reverse columns order in a table with python ?

Example of how to reverse columns order in a table with python: let's consider the following table: >>> import numpy as np >>> table = np.array([[0., 1., 2., 3., 4., 5., 6., 7.], ... [6., 6., 4., 9., 7., 5., 4., 8.], ... [4., 9., 3., 7., 5., 2., 3., 1.], ... [6., 9., 8., 1., 4., 3., 2., 1.], ... [4., 8., 7., 6., 1., 7., 1., 1.]]) \begin{equation} \begin{array}{cccccc} 0. & 1. & 2. & 3. & 4. & 5. & 6. & 7.\\\\ 6. & 6. & 4. & 9. & 7. &

Comment inverser l'ordre des colonnes d'un tableau en python avec numpy ?

Exemples de comment inverser l'ordre des colonnes d'un tableau en python avec numpy. Soit un tableau quelconque: >>> import numpy as np >>> table = np.array([[0., 1., 2., 3., 4., 5., 6., 7.], ... [6., 6., 4., 9., 7., 5., 4., 8.], ... [4., 9., 3., 7., 5., 2., 3., 1.], ... [6., 9., 8., 1., 4., 3., 2., 1.], ... [4., 8., 7., 6., 1., 7., 1., 1.]]) \begin{equation} \begin{array}{cccccc} 0. & 1. & 2. & 3. & 4. & 5. & 6. & 7.\\\\ 6. & 6. & 4. &

How to reverse rows order in a table with python ?

Examples of how to reverse rows order in a table with python. Let's consider a random table: >>> import numpy as np >>> table = np.array([[0., 4., 4., 2., 7., 0.], ... [1., 2., 7., 2., 3., 4.], ... [2., 2., 3., 3., 6., 3.], ... [3., 9., 0., 6., 3., 3.], ... [4., 3., 1., 8., 2., 5.], ... [5., 9., 3., 7., 6., 8.], ... [6., 9., 5., 8., 1., 8.], ... [7., 9., 6., 7., 3., 7.], ... [8., 2., 1., 4., 4., 8.], ... [9.,

Comment inverser l'ordre des lignes d'un tableau en python avec numpy ?

Exemples de comment inverser l'ordre des lignes d'un tableau en python avec numpy. Soit un tableau quelconque: >>> import numpy as np >>> table = np.array([[0., 4., 4., 2., 7., 0.], ... [1., 2., 7., 2., 3., 4.], ... [2., 2., 3., 3., 6., 3.], ... [3., 9., 0., 6., 3., 3.], ... [4., 3., 1., 8., 2., 5.], ... [5., 9., 3., 7., 6., 8.], ... [6., 9., 5., 8., 1., 8.], ... [7., 9., 6., 7., 3., 7.], ... [8., 2., 1., 4., 4., 8.],

How to find the minimum or maximum value in a matrix with python ?

Examples of how to find the minimum and maximum values in a matrix using numpy and python: [TOC] ### Approach 1: To find the minimum and maximum values in an array, a solution is to use the numpy functions max() and min(): >>> import numpy as np >>> A = np.array(([2,7,-1],[1,9,8],[-1,5,6])) >>> A array([[ 2, 7, -1], [ 1, 9, 8], [-1, 5, 6]]) >>> np.min(A) -1 >>> np.max(A) 9 ### Approach 2: Another solutio

How to rotate an image using python ?

Examples of how to rotate an image using python: [TOC] ### Rotate an image and keep the same size as the original image To rotate an image, a solution is to use the pillow function [rotate()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example [images:eiffel-tower;eiffel-tower-rotate-01 dim:1*2 size:75 caption:How to rotate an image using python ?] from PIL import Image im = Image.open("eiffel_tower.jpg") print(im.size) im = im.rotate(45) print(i

Comment tourner une image en python avec le module pillow ?

Exemples de comment tourner une image en python avec le module pillow [TOC] ### Tourner une image en gardant la même taille Pour tourner une image, il existe la fonction pillow [rotate()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), exemple [images:eiffel-tower;eiffel-tower-rotate-01 dim:1*2 size:75 caption:Comment tourner une image en python avec le module pillow ?] from PIL import Image im = Image.open("eiffel_tower.jpg") print(im.size) im = im.rota

Comment retourner une image horizontalement en python ?

Exemples de comment retourner une image horizontalement en python [TOC] ### Obtenir une image retournée horizontalement avec pillow Pour retourner une image horizontalement en python, il existe la fonction pillow [mirror()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example: from PIL import Image from PIL import ImageOps im = Image.open("lena.png") im = ImageOps.mirror(im) im.save("lena_mirror.png") im.show() [images:458;lena-mirror dim:1*2 size

How to flip an image horizontally in python ?

Examples of how to flip an image horizontally in python: [TOC] ### Get a vertically flipped image using pillow To flip an image horizontally a solution is to use the function [mirror()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example: from PIL import Image from PIL import ImageOps im = Image.open("lena.png") im = ImageOps.mirror(im) im.save("lena_mirror.png") im.show() [images:458;lena-mirror dim:1*2 size:75 caption:How to flip an image horiz

How to flip an image vertically in python ?

Examples of how to flip an image vertically in python: [TOC] ### Get a vertically flipped image using pillow To flip an image vertically a solution is to use the function [rotate()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example: from PIL import Image im = Image.open("eiffel_tower.jpg") im = im.rotate(180) im.save("eiffel_tower_fliped_vertically.jpg") [images:eiffel-tower;eiffel-tower-fliped-vertically dim:1*2 size:75 caption:How to flip an imag

How to read a VIIRS L2 CLDPROP netcdf file using python ?

An example of how to read a VIIRS L2 CLDPROP netcdf file using python 3: [TOC] ### Download a VIIRS L2 CLDPROP netcdf file To download a VIIRS L2 CLDPROP netcdf file, a solution is to go on [LAADS DAAC](https://ladsweb.modaps.eosdis.nasa.gov/search/order/2/CLDPROP_L2_VIIRS_SNPP--5110/2019-01-01/DB/World). Example of granule selected randomly that will be used hereafter: CLDPROP_L2_VIIRS_SNPP.A2019001.1742.001.2019062201640.nc ### Read a netCDF file with python To read the C

How to calculate the inverse of a matrix in python using numpy ?

To calculate the inverse of a matrix in python, a solution is to use the linear algebra numpy method [linalg](https://docs.scipy.org/doc/numpy/reference/routines.linalg.html). Example \begin{equation} A = \left( \begin{array}{ccc} 1 & 3 & 3 \\\\ 1 & 4 & 3 \\\\ 1 & 3 & 4 \end{array}\right) \end{equation} inverse matrix A_inv \begin{equation} A^{-1} = \left( \begin{array}{ccc} 7 & -3 & -3 \\\\ -1 & 1 & 0 \\\\ -1 & 0 & 1 \end{array}\right) \end{equation} >>> import

How to invert the elements of a boolean array in python ?

Examples of how to invert the elements of a boolean array in python using the numpy function [invert()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html) >>> import numpy as np >>> a = np.array((True,True,False,True,False)) >>> b = np.invert(a) >>> b array([False, False, True, False, True], dtype=bool) Another example >>> import numpy as np >>> a = np.array((1,1,0,1,0), dtype=bool) >>> b = np.invert(a) >>> b array(

Comment inverser les éléments d'une matrice booléenne avec numpy de python ?

Exemples de comment inverser les éléments d'une matrice booléenne en utilisant la fonction numpy [invert()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html) >>> import numpy as np >>> a = np.array((True,True,False,True,False)) >>> b = np.invert(a) >>> b array([False, False, True, False, True], dtype=bool) Autre exemple: >>> import numpy as np >>> a = np.array((1,1,0,1,0), dtype=bool) >>> b = np.invert(a) >>> b arra

How to overlay / superimpose two images using python and pillow ?

Examples of how to overlay / superimpose two images using python and pillow [TOC] ### Overlay two images of same size To overlay two images in python, a solution is to use the pillow function [paste()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.paste), example: from PIL import Image import numpy as np img = Image.open("data_mask_1354_2030.png") background = Image.open("background_1354_2030.png") background.paste(img, (0, 0), img) ba

Comment superposer deux images avec le module python pillow ?

Exemples de comment superposer deux images avec le module python pillow: [TOC] ### Superposer deux images de même taille Pour superposer deux images de même taille on peut utiliser la fonction pillow [paste()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.paste), exemple: from PIL import Image import numpy as np img = Image.open("data_mask_1354_2030.png") background = Image.open("background_1354_2030.png") background.paste(img, (0, 0), i

How to mask an array using another array in python ?

Examples of how to mask/select elements of an array using another array in python [TOC] ### Mask an array from another array To mask an array, there are several approaches with numpy (see the module called [ma](https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html)). A solution for example is to use [numpy.ma.masked_where](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ma.masked_where.html) to mask the elements of the array x if the elements of the ar

How to create a sitemap for a website based on Django ?

An example of how to create a sitemap for a website based on [Django](https://docs.djangoproject.com/en/2.2/ref/contrib/sitemaps/) [TOC] Let's assume a Django website with an application called "notebooks" with the following files: database.db manage.py myproject / settings.py urls.py notebooks / views.py models.py urls.py with models.py class Article(models.Model): full_path = models.TextField() date_created = models.DateTimeField(default=datetime

Comment créer un sitemap avec le web-framework Django ?

Exemple de comment créer un sitemap pour un site web basé sur [Django](https://docs.djangoproject.com/en/2.2/ref/contrib/sitemaps/) (version utilisée 1.11.10): [TOC] On va ici supposer un site avec une application intitulée "notebooks" avec l'organisation suivante: database.db manage.py myproject / settings.py urls.py notebooks / views.py models.py urls.py avec models.py class Article(models.Model): full_path = models.TextField() date_created = mod