Found 10476 Articles for Python

How to add a Money field in Django?

Ath Tripathi
Updated on 25-Aug-2021 12:37:19

2K+ Views

Sometimes, we may have to add money-related data in a website, like salary, fees or income. Django provides an integer field but many a time, it doesn't work like we want. So, for handling money field, we can use a third-package library that will add the money field to our model.Make a project and an app, I named it "MoneyFieldDemo" and "myapp".Set the basic things like urls and INSTALLED_APPS.And yes, install a library −pip install django-moneyAdd the following line in settings.py −INSTALLED_APPS+= ["djmoney"]ExampleIn app's, urls.py, add the following lines −from django.urls import path from . import views urlpatterns = ... Read More

Adding JSON field in Django models

Ath Tripathi
Updated on 25-Aug-2021 12:33:02

15K+ Views

In this article, we will see how to add JSON fields to our Django models. JSON is a simple format to store data in key and value format. It is written in curly braces. Many a time, on developer website, we need to add developer data and JSON fields are useful in such cases.First create a Django project and an app. Please do all the basic things, like adding app in INSTALLED_APPS and setting up urls, making a basic model and render its form in an HTML file.ExampleInstall the django-jsonfield package −pip install django-jsonfieldNow, let's create a model in models.py, ... Read More

How to add extra security to Django admin using fake admin login?

Ath Tripathi
Updated on 25-Aug-2021 12:31:17

389 Views

We are going to make a Django admin fake login page using a thirdparty package. This will just create a Django admin fake page, and whenever anyone tries to login on the admin page, whether they enter the right or wrong password, they will not be able to login and their trial with their IP addresses will be stored in a table.So, just follow the steps given below and you will be all good to go.Setup basic urls and add app in INSTALLED_APPS in settings.py.ExampleFirst install the packagepip install django-admin-honeypotIn settings.py, add this −INSTALLED_APPS+ = ['admin_honeypot']We simply add it to ... Read More

How to add Django debug toolbar to your project?

Ath Tripathi
Updated on 25-Aug-2021 12:18:58

3K+ Views

Django toolbox is a debugging tool that is used to debug database queries, Django website loading speed, and many other things. Debug toolbar is very popular among developers and everyone is using it. So, let's dive into to see how to implement it.ExampleCreate an app with the name "myapp".First, install the django-debug-toolbar −pip install django-debug-toolbarNow, add 'debug_toolbar' to your INSTALLED_APPS in settings.py −INSTALLED_APPS = [    # ...    'debug_toolbar',    'myapp' ]This will add the debug toolbar as an app in our project.Next, in your middleware, add the following −MIDDLEWARE = [    # ...    'debug_toolbar.middleware.DebugToolbarMiddleware',    # ... Read More

How to create a 100% stacked Area Chart with Matplotlib?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 08:03:50

1K+ Views

To create a 100% stacked Area Chart with Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a list of years.Make a dictionary, with list of population in respective years.Create a figure and a set of subplots.Draw a stacked Area Plot.Place a legend on the figure, at the location ''upper left''.Set the title, xlabel and ylabel.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True year = [1950, 1960, 1970, 1980, 1990, 2000, 2010, 2018] population_by_continent = {   ... Read More

How to understand Seaborn's heatmap annotation format?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 08:34:41

2K+ Views

To understand Seaborn's heatmap annotation format, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe with five columns.Plot the rectangular data as a color-encoded matrix, fmt=".2%" represents the annotation format.To display the figure, use show() method.ExampleExampleimport seaborn as sns import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(np.random.random((5, 5)), columns=["a", "b", "c", "d", "e"]) sns.heatmap(df, annot=True, annot_kws={"size": 7}, fmt=".2%") plt.show()OutputRead More

How to remove or hide X-axis labels from a Seaborn / Matplotlib plot?

Rishikesh Kumar Rishi
Updated on 12-Sep-2023 01:10:01

40K+ Views

To remove or hide X-axis labels from a Seaborn / Matplotlib plot, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Use sns.set_style() to set an aesthetic style for the Seaborn plot.Load an example dataset from the online repository (requires Internet).To hide or remove X-axis labels, use set(xlabel=None).To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True sns.set_style("whitegrid") tips = sns.load_dataset("tips") ax = sns.boxplot(x="day", y="total_bill", data=tips) ax.set(xlabel=None) plt.show()OutputRead More

How to remove whitespaces at the bottom of a Matplotlib graph?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 08:43:20

4K+ Views

To remove whitespaces at the bottom of a Matplotlib graph, we can use tight layout or autoscale_on=False.stepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement.Plot a list of data points using plot() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111, autoscale_on=False, xlim=(1, 5), ylim=(0, 10)) ax.plot([2, 5, 1, 2, 0, 7]) plt.show()OutputRead More

How to extract only the month and day from a datetime object in Python?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 08:51:27

1K+ Views

To extract only the month and day from a datetime object in Python, we can use the DateFormatter() class.stepsSet the figure size and adjust the padding between and around the subplots.Make a dataframe, df, of two-dimensional, size-mutable, potentially heterogeneous tabular data.Create a figure and a set of subplots.Plot the dataframe using plot() method.Set the axis formatter, extract month and day.To display the figure, use show() method.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt, dates plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), speed=np.linspace(1, 10, 10))) fig, ax = ... Read More

How to remove the first and last ticks label of each Y-axis subplot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 08:56:18

3K+ Views

To remove the first and last ticks label of each Y-axis subplot, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Iterate the axes and set the first and last ticklabel's visible=False.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots(2, sharex=True) for a in ax:    plt.setp(a.get_yticklabels()[0], visible=False)    plt.setp(a.get_yticklabels()[-1], visible=False) plt.show()OutputRead More

Advertisements