profile-image
Benjamin H.G. Marchant (Open Notebook)

How to install tensorflow on a mac studio ?


Recently bought a new mac studio and needed Tensorflow for my work. On my previous iMac I installed tensorflow using Anaconda and it worked very well. However now with my new mac studio (apple M1 chip), I got the following error on a jupyter noteboook: "the kernel appears to have died. it will restart automatically" when I do: import tensorflow as tf And if I launch python on the terminal: import tensorflow as tf gives ERROR: Could not find a version that sat ...


Created Dec. 3, 2022, 1:20 a.m.
Tags Python; TensorFlow; Mac;

How to check if a pandas dataframe is empty in python ?


Examples of how to check if a pandas DataFrame is empty: [TOC] ### Create an empty dataframe Let's create an empty dataframe with pandas import pandas as pd df = pd.DataFrame() print(df) gives Empty DataFrame Columns: [] Index: [] ### Check if dataframe is empty To check if a dataframe is empty a solution is to use "empty": df.empty returns True here. With a if statement: if df.empty: print("Oups, your ...


Created Dec. 3, 2022, 12:31 a.m.
Tags Python; Pandas; Dataframe;

How to pass optional arguments in a python function ?


Examples of how to pass optional arguments in a python function? [TOC] ### Basic python function Let's start by creating a simple function in python: def function_test(a,b): print(a,b) return None myfunc(1,2) gives 1 2 However if we try to pass a third variable myfunc(1,2,7) will generate an error message too many values to unpack (expected 2) def myfunc(a,b): print('function print', a,b) return a ...


Created Nov. 29, 2022, 10:14 p.m.
Tags Python; Function;
Language(s): fr

How to iterate over the first n rows of a pandas DataFrame in python ?


Examples of how to iterate over the first n rows of a pandas DataFrame in python [TOC] ### Create a pandas dataframe Let's first create a DataFrame: import pandas as pd import numpy as np data = np.arange(1,31) data = data.reshape(10,3) df = pd.DataFrame(data, columns=['A','B','C']) print(df) gives A B C 0 1 2 3 1 4 5 6 2 7 8 9 3 10 11 12 4 13 14 15 5 16 17 18 6 19 20 ...


Created Nov. 29, 2022, 9:43 p.m.
Tags Python; Pandas; Dataframe;
Language(s): fr

How to create a pandas dataframe from a python dictionary ?


Examples of how to create a pandas dataframe from a python dictionary: [TOC] ### Create a dataframe from a python dictionary (method 1) To create a dataframe from a python dictionary: d = { 'Name': ['Ben', 'John', 'Emma', 'Zoe'], 'Age': [40, 56, 34, 12] } a solution is to do import pandas as pd df = pd.DataFrame(d) gives Name Age 0 Ben 40 1 John 56 2 Emma 34 3 Zoe 12 ### Create a dataframe ...


Created Nov. 27, 2022, 3:29 a.m.
Tags Python; Pandas; Dataframe; Dictionary;
Language(s): fr

How to plot a rectangle on a cartopy map with python ?


Examples of how to plot a rectangle on a cartopy map with python: [TOC] ### Example 2 (PlateCarree projection) from matplotlib.pyplot import figure import matplotlib.pyplot as plt import matplotlib.patches as mpatches import cartopy.crs as ccrs fig = figure(num=None, figsize=(12, 10), dpi=100, edgecolor='k') proj = ccrs.PlateCarree() ax = plt.axes(projection=proj) ax.set_global() long = -70. lat = -45. ax.add_patch(mpat ...


Created Nov. 26, 2022, 3:41 p.m.
Tags Python; Cartopy; Map; Rectangle;
Language(s): fr

How to plot a circle on a cartopy map with python ?


Examples of how to plot a circle on a cartopy map with python: [TOC] ### Plot a red circle using PlateCarree projection To plot a circle on a cartopy map, a solution is to use matplotlib [patches](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.patches.Circle.html) : from matplotlib.pyplot import figure import numpy as np import matplotlib.pyplot as plt import cartopy.crs as ccrs import matplotlib.patches as mpatches fig = figure(num=None, figsize ...


Created Nov. 26, 2022, 3:48 a.m.
Tags Python; Cartopy; Circle;
Language(s): fr

How to minify html or css files for improving web site speed using python ?


Examples of how to minify html or css files for improving web site speed using python: [TOC] ### Minify html using htmlmin A solution is to use the python module [htmlmin](https://pypi.org/project/htmlmin/) that can be installed using pip: pip install htmlmin or [conda-forge](https://anaconda.org/conda-forge/htmlmin) conda install -c conda-forge htmlmin Then to minify a html text: import htmlmin html_txt = '''<!DOCTYPE html> <html> <body> ...


Created Nov. 25, 2022, 3:43 a.m. | Edited Nov. 25, 2022, 7:26 p.m.
Tags Python; HTML; CSS; Minify;
Language(s): fr

How to merge two folders using python ?


Example of how to merge two folders using python [TOC] ### Introduction I used google takeout to download a project that has been saved on google drive. However since the project is big, the project has been divided in two folders during the download: Takeout_01 and Takeout_02 folders. Folders paths in my local computer: takeout_01_path = '/Users/JDoe/Downloads/Takeout_01' takeout_02_path = '/Users/JDoe/Downloads/Takeout_02' The goal was then to ...


Created Nov. 24, 2022, 4:59 a.m. | Edited Nov. 24, 2022, 3:10 p.m.
Tags Python; Linux;
Language(s): fr

How to switch dictionary keys and values in python ?


Example of how to switch dictionary keys and values in python: [TOC] ### Switching dictionary keys and values Let's consider the following dictionary: d = {'A':1, 'B':2, 'C':3} To iterate over all keys and values we can use [items()](https://docs.python.org/3/tutorial/datastructures.html): for x, y in d.items(): print(x,y) gives A 1 B 2 C 3 Then to create a new dictionary with inverted keys and values a straigtforward solution is to do: ...


Created Nov. 23, 2022, 2:43 p.m.
Tags Python; Dictionary;
Language(s): fr

How to list and download all files from a url directory using python ?


Example of how to list all files from an url directory and how to download them: [TOC] ### Introduction Example of case: a colleague sent you a url link (for example 'https://******/pub/') with a list of files (see image below). The goal here is to list and download all files that ends with ".nc": [image:list-and-download-all-files-from-url-python size:75 caption:How to list and download all files from an url directory using python ?] ### List all files under the url directory ...


Created Oct. 26, 2022, 3:39 p.m. | Edited Nov. 21, 2022, 12:16 a.m.
Tags Python; urlretrieve; BeautifulSoup;
Language(s): fr

How to download a file from a public (no credentials) AWS S3 bucket with python ?


Example of how to download a file from a public, no credentials required here, AWS S3 bucket with python ? [TOC] ### Find an open data project Let's consider the [NOAA](https://registry.opendata.aws/noaa-jpss/) public s3 bucket. We want to download a file from the following folder [SNPP/VIIRS/SNPP_AF_I-Band_EDR_NRT/2022/09/09/](https://noaa-jpss.s3.amazonaws.com/index.html#SNPP/VIIRS/SNPP_AF_I-Band_EDR_NRT/2022/09/09/). ### Install boto3 To download a file from a aw S3 bucket with ...


Created Oct. 12, 2022, 12:16 a.m. | Edited Nov. 14, 2022, 3:24 a.m.
Tags Python; AWS S3;
Language(s): fr

How to create your own sitemap using a view with Django ?


Example of how to create your own sitemap using a view with Django: [TOC] Note: Django comes with a high-level sitemap-generating framework to create sitemap XML files.[see](https://docs.djangoproject.com/en/4.1/ref/contrib/sitemaps/). However, I had to create my own sitemaps to deal with subdomains. ### Create a sitemap Lets consider for example a website with an ensemble of articles stored in the following model: class Article(models.Model): url = models.CharField( ...


Created Sept. 27, 2022, 2:13 a.m.
Tags Django; Sitemap;

How to test sub-domains locally with django (on Linux or Mac) ?


Examples of how to test sub-domains locally with django: [TOC] ### Edit hosts file By default to test locally a django app, it is possible to run a server python manage.py runserver and to go to the url localhost:8000 Now, we want to test sub-domains, for example : www.localhost:8000 fr.localhost:8000 es.localhost:8000 . . . To do that open the hosts file locatedin the etc folder: vi /etc/hosts gives for example: ...


Created Sept. 26, 2022, 6:15 p.m.
Tags Django;

How to fix Django error: Attempt to write a readonly database ?


Got the following error "Attempt to write a readonly database" with a django web site after replacing the database. Django app has been deployed on a digitalocean ubuntu droplet using Nginx and Gunicorn. [TOC] ### Change database ownership Problem was that the django app has been deployed on a repertory owned by a user called for example 'johndoe': /home/johndoe/ however the database owner was "root" ls returns -rw-rw-r-- 1 root root 116928512 ...


Created Sept. 26, 2022, 4:38 p.m.
Tags Django;

How to delete a row in a table with Django ?


Example of how to delete a row in a table with Django [TOC] ### Deleting a row in a table To delete for example a user, a solution is to use [delete()](https://docs.djangoproject.com/en/4.1/topics/db/queries/#deleting-objects). Select for example the user with id = 1: q = User.objects.get(pk=1) then enter q.delete() Note: before deleting a row, it is important to know how on_delete has been defined in the database. Check the models.py file [What does on_delete ...


Created Sept. 11, 2022, 12:19 a.m.
Tags Django;

How to get all field names of a table with Django ?


Examples of how to get all field names of a table with Django (SQLite): [TOC] ### Using Model meta API To get all field names of a table with Django, a solution is to use [Model _meta API](https://docs.djangoproject.com/en/4.1/ref/models/meta/): table_name._meta.get_fields() For example let's consider an application called notebook with a table named Article (models.py): class Article(models.Model): notebook = models.ForeignKey(Notebook, null=True, blank=True, ...


Created Sept. 10, 2022, 3:01 a.m.
Tags Django; Database;

How to convert a column of float (quantitative data) into categorical data with pandas using cut ?


Examples of how to convert quantitative data to categorical data with pandas using cut: [TOC] ### Create synthetic data Let's first create some fake continuous data: import random l = [random.randint(0,100) for i in range(10)] returns for example [66, 44, 62, 99, 82, 13, 7, 58, 60, 38] Save data in a pandas dataframe import pandas as pd import numpy as np data = np.array(l) df = pd.DataFrame(data,columns=['x']) print(df) ret ...


Created Sept. 9, 2022, 2:23 a.m.
Tags Python; Pandas; DataFrame;

How to insert an image encoded in base64 in a jupyter notebook ?


Example of how to insert an image encoded with base64 in a jupyter notebook ? [TOC] ### Encoding an image to base64 string represenation in python First step is to convert a given image to a string encoded in base64, a solution is to use the python module pybase64. Installation with pip pip install pybase64 Installation with conda conda install -c conda-forge pybase64 ### Convert a png image to base64 Now it is possible to convert an image called for example i ...


Created Sept. 2, 2022, 2:11 p.m.
Tags Python; Jupyter Notebook;

How to convert an image (png, jpg, etc) to base64 string using python ?


Example of how to convert a png image to base64 using python: [TOC] ### Install pybase64 module with pip pip install pybase64 with conda conda install -c conda-forge pybase64 ### Convert a png image to base64 import base64 with open("img.png", "rb") as img_file: encoded_string = base64.b64encode(img_file.read()) print(encoded_string) returns for example b'iVBORw0KGgo...ozAAAAABJRU5ErkJggg==' ### Add an image encoded in b ...


Created Sept. 2, 2022, 2:45 a.m.
Tags Python; Base64;

How to create a new file in Linux ?


Examples of how to create a new file in Linux: [TOC] ### Create a new file using touch To create a new file in linux a straightforward solution is to use the command [touch](https://linuxhint.com/touch-command-linux/): touch new_file.txt touch accept options such as: -c or –no-create: Not creating the file if it doesn’t exist. -m: Changes the modification time. ### Using linux > output operator Another solution is to use the operator >: linux_cmd > fil ...


Created Sept. 2, 2022, 2:11 a.m.
Tags Linux;

How to plot a heatmap over polar regions using cartopy, matplotlib and python ?


Examples of how to plot a heatmap over polar regions using cartopy, matplotlib and python: [TOC] ### Plot a heatmap over antarctica using cartopy (example 1) import cartopy.feature import matplotlib.path as mpath fig = plt.figure(figsize=[10, 5]) ax1 = plt.subplot(1, 2, 1, projection=ccrs.SouthPolarStereo()) # Limit the map to -60 degrees latitude and below. ax1.set_extent([-180, 180, -90, -60], ccrs.PlateCarree()) ax1.add_feature(cartopy.feat ...


Created Sept. 1, 2022, 8:16 p.m.
Tags Python; Matplotlib; Cartopy;

How to calculate the sine of an angle in python ?


Examples of how to get the sine of an angle in python [TOC] ### Using python math module To calculate the sine of an angle in python, a solution is to use the python [math](https://docs.python.org/3/library/math.html#math.sin) module: import math angle = 0.7 # angle in radians math.sin(angle) returns 0.644217687237691 Another example angle = 0. # angle in radians math.sin(angle) returns 0.0 Note to get pi number value: ...


Created Sept. 1, 2022, 1:44 a.m.
Tags Python; Math;

How to calculate the cosine of an angle in python ?


Examples of how to get the cosine of an angle in python [TOC] ### Using python math module To calculate the cosine of an angle in python, a solution is to use the python [math](https://docs.python.org/3/library/math.html#math.cos) module: import math angle = 0.7 # angle in radians math.cos(angle) returns 0.7648421872844885 Another example angle = 0. # angle in radians math.cos(angle) returns 1.0 Note to get pi number value: ...


Created Aug. 31, 2022, 7 p.m.
Tags Python; Math;

How to embed a google map in a HTML page ?


Example of how to embed a google map in a HTML page [TOC] ### Create an html file First step, let's create a simple html file: <!doctype html> <html> <head> <title>Embedding a google map</title> </head> <body> </body> </html> Now add between bodytags : <iframe width="450" height="450" frameborder="0" style="border:0" referrerpolicy="no-referrer-when-downgrade" src="https://www.goo ...


Created Aug. 31, 2022, 6:07 p.m.
Tags Google; Maps; HTML;

Search: