Astra DB Vector Database - Django Application Integration
In this post, we'll learn how to take an Astra DB database that has been pre-populated with vector data, and connect that database to a Django application. We will build an app that lets users view articles, and send customized queries to an LLM that uses Retrieval Augmented Generation from Astra DB to enhance the responses returned from that LLM.
Astra DB Vector Database - Langchain integration and Retrieval Augmented Generation
In this post, we'll show how to create, setup and populate an Astra DB vector database. We will show how to integrate Astra DB with Langchain to create and store vectors, and perform Retrieval Augmented Generation with LLMs in order to get better results when performing question-answering tasks with AI chatbots.
Retrieval Augmented Generation with Langchain and PGVector
In this post, we'll look at how to do Retrieval Augmented Generation with LangChain and PGVector / PostgreSQL. This technique allows us to take a user question or query, and select the most similar (i.e. most relevant) documents from a vector database, and then use those documents as additional context to a call to a large language model (LLM).
Rendering Template Fragments with django-template-partials
This post looks at the django-template-partials library, a package developed by Carlton Gibson that allows you to render HTML template fragments in response to requests in Django. This can be paired with hypermedia-driven libraries such as HTMX in order to create engaging applications that avoid extensive JavaScript dependencies!
Vector Databases - pgvector and langchain
This post will dive into the concept of a vector database, and will look at how Langchain can take a piece of text, chunk it into vectors, and store those vectors in an underlying database. We will use pgvector to store our embeddings, and will see how to query these embeddings and find the closest existing vectors to a given point.
Langchain, Streamlit and OpenAI - Meal Planning with AI
In this post, we introduce an AI-powered meal planner that simplifies the process of meal planning. By leveraging the capabilities of OpenAI's language model and the "langchain" library, the application generates meal suggestions based on a list of ingredients provided by the user. A Streamlit UI allows users to input their ingredients and generate meal suggestions with just a click of a button.
Streamlit & PandasAI - Prompt-Driven Data Analysis
In this post, we will explore how to build a Streamlit application that uses PandasAI, and let's users prompt for insights into a dataset. The application enables users to upload a CSV file, and then input a text-based prompt to query the dataset with PandasAI, and then view (or visualize) the results.
Pydantic - Validators
In this post, we dive further into Pydantic validators, including how to use the @root_validator decorator, how to define validators that run before others, and how to run validators on each item in a sequence of data.
Pydantic - Field function and Model Config
In this post, we'll dive deeper into Pydantic's features and learn how to customize fields using the Field() function. We can use this to set default values, to include/exclude fields from exported model outputs, to set aliases, and to customize the model's JSON schema output. We'll also learn about model Config classes, which can be used to customize model-wide behavior.
Pydantic - Nested Models and JSON Schemas
In this post, we'll learn about how to implement Nested Models in Pydantic model classes, including how to do validations on the child models, for example using unions, literals and constrained lists. We'll also learn how to generate JSON schemas from our Pydantic models, and how to use datamodel-code-generator to generate Pydantic models from JSON Schema definitions.
Introduction to Pydantic
Pydantic is a Python library that handles data validation and settings management using type-annotated class-fields. In this post, we will cover the basics of Pydantic, and see how to use it to model and validate JSON data coming from an external source. We'll see how to apply constrained fields, write custom field validations, and how to export models to JSON/dictionaries.
Deploying Django to AWS Elastic Beanstalk
In this post, we learn how to deploy a basic Django application to the AWS Elastic Beanstalk service. We'll learn how to deploy the application, will learn about the Elastic Beanstalk platform and the CLI tool, and will learn how to set up static file serving.
Template Fragments with Django and HTMX
In this post, we will look at how to render fragments of a template when using Django, as an alternative to splitting content out into separate HTML partials. For example, when returning HTML in an AJAX response, we might not always want to extract partial templates - instead, we can declare blocks in our main Django template, and render only the content within those blocks.
Django and Webpack - Bundle Caching, Multiple Bundles, and the HtmlWebpackPlugin
In this post, we'll cover some more advanced Webpack concepts - using content hashes in bundle names for caching, generating multiple bundles (vendor/3rd party vs source bundles) and using the HtmlWebpackPlugin.
Django URLs - templatetags, reversing URLs, namespacing and get_absolute_url()
In this post, we'll dive into some of the URL handling in Django, including looking at best practices around linking to URLs in both HTML templates and Python code. We'll look at namespacing URL configurations in different Django apps within a project, as well as a brief look at the get_absolute_url() method that can be defined on your Django models.
Django and Webpack - TypeScript, Babel, and Asset Modules
In this post, we'll learn how to leverage Webpack to use TypeScript, and ES2015+ JavaScript in our Django applications. We'll learn how to use Babel to transpile ES2015+ JavaScript into a format understood by browsers, and we will also learn about Webpack 5 asset modules that allow us to load different assets into our bundles and outputs.
Django and Webpack - External Libraries, Loaders and Stylesheets
In this post, we'll extend our knowledge of Webpack and learn how to include external libraries, and how to include CSS stylesheets in our final bundles. We will also learn what Webpack Loaders are, and where you can apply these in your build pipeline.
Django and Webpack - Setup and Basic Usage
In this video, we discuss how to set Webpack up in the context of a Django application, and how to use Webpack's core features to bundle together JavaScript files.
Introduction to Alpine.js
In this post, we will introduce the Alpine.js library - this is a lightweight JavaScript framework for building interactive applications that circumvents the need for complex JavaScript tooling. It's directives are inspired by Vue.js, but Alpine.js has a more minimal set of functionality, making it a good option for building interactive apps with minimal JavaScript overhead.
Creating Scheduled Tasks with Django-Crontab
In this video we'll demonstrate how to create a scheduled task using the django-crontab package. We will create a task that fetches cryptocurrency data from the CoinMarketCap API and updates the database. We will also demonstrate how to set these cronjobs up within a Docker container that runs our Django application
Python Primers - Moving Old Files To ZIP Archives
In this Python Primer, we will walk through the process of moving all files in the current directory that are older than a certain date to a ZIP file.
Django and HTMX Forms - with django-forms-dynamic
In this post, we'll look at a new Django Form library called django-forms-dynamic. This library "resolves form field arguments dynamically when a form is instantiated, not when it's declared" - allowing dynamic forms to be created, where the values of one field can be dependent on another. This makes the library suitable for use with tools such as HTMX and Unpoly.
Django, Bokeh and HTMX - Data Driven Line Charts
In this video, we will create a dynamic, database-driven LINE chart for a country's historical GDP data. We will use Django for our backend functionality, will use Bokeh to create a line chart showing the trajectory of this GDP data, and will use HTMX to dynamically filter the country whose data is shown on the chart. We will also show how to create multi-line charts using Bokeh.
Django, Bokeh and HTMX - Data Driven Bar Charts
In this video, we will create a dynamic, database-driven bar chart for country GDP data. We will use Django for our backend functionality, will use Bokeh to create bar charts representing the GDP data, and will use HTMX to dynamically filter the year for which data is shown.
Django and HTMX Chained Dropdown
In this video, we build a simple chained dropdown using Django and HTMX. We will look at a simple scenario where the first dropdown contains a set of university courses, and the second dropdown contains a set of modules for that course. The contents of the second dropdown depend on the selection of the first dropdown, and we will use HTMX to perform actions when an option in the first dropdown is selected.
Python Filesystem Operations
In this post, we will demonstrate how to perform certain actions on the filesystem, such as listing files and directories, listing all files with a particular extension/type, sorting files alphabetically, sorting files by the file size, and creating a ZIP file from selected files.
Scraping Historical Champions League Data
In this post, we will use the BeautifulSoup and requests libraries in Python to scrape historical Champions League data, from a specific year until the present day. This post will be the basis for a future post, where we plot out a bar-chart-race based upon this scraped data.
Django and HTMX Live Scores Project #2
In this post, we'll extend our application to include HTMX-driven search functionality and visual feedback to the user when HTMX polling requests are updating the UI.
Django and HTMX Live Scores Project
In this post, we will build an application using Django and HTMX that simulates live sports scores streaming into a webpage. The application will use HTMX's polling capabilities for this, and will dynamically update a frontend page with new scores and full-time results as they come in.
NASA CLI App with Python - Concurrent Requests with Asyncio and HTTPX
In this post, we will convert our sequential network requests to a more efficient, concurrent model, using the HTTPX library for network calls, and the standard-library's asyncio module.
NASA CLI App with Python - Fetching Range of Images
In this tutorial, we will extend our setup to give users the ability to fetch images between a start date and an end date. We will add these two CLI options to our Typer command, and will use these to determine the action that the command should take.
NASA CLI App with Python - Fetching Images
This post will extend the original Typer tutorial to fetch images from the NASA API, and display them on the user's desktop. We will make use of a new Typer command, and will add default arguments and the ability to parse both JSON and bytes (image) responses from NASA's API.
NASA CLI App with Typer and Python
In this post, we will explore the Typer package, which is a modern package for developing command-line applications in Python. Typer provides decorators for defining commands, and uses modern Python type-hinting for command-line arguments and options.
Django and HTMX - Delete Page
In this post, we'll extend the list/create page to give users the ability to delete films from their list. We will do this using HTMX, using AJAX requests to remove the film from the users list and insert the returned template into the DOM without refreshing the page
Django and HTMX - List And Create Page (with no refreshes!)
In this post, we will build a page allowing users to list out their favourite movies, and add new movies to their list. This will work without any page refreshes, affording the user a better and more reactive experience, thanks to HTMX.
NetworkX - Algebraic Connectivity, Bridges, and Connected Components
In this post, we will analyze the algebraic connectivity of a graph, and will understand how this value relates to the connectivity of the graph. We will also dive into some core graph theory concepts, such as graph density, connected components and graph bridges in this post. We will see how to use NetworkX to analyze these core concepts.
Django and HTMX Trigger Modifiers & CSS Transitions
In this post, we will learn how to use trigger modifiers and CSS transitions in HTMX. We will extend the previous tutorial's example, and demonstrate how to add trigger modifiers to the keyup event on our registration form's username field. We will also use CSS transitions to animate the message displayed to the user.
Django and HTMX
In this post, we will take a look at an example of using HTMX within a Django application. Using HTMX allows us to perform AJAX requests to our Django backend and swap out content in the DOM - all without writing a single line of JavaScript.
Creating Graphs with NetworkX
This post looks at how to create simple, undirected graphs in NetworkX, and looks at the utilities the library provides for adding and removing nodes and edges from a graph. We also look at how to inspect basic features of a graph, such as its nodes, edges, degrees of nodes, and node neighbors.
Properties of the Laplacian Matrix
In this post, we will look at some algebraic properties of the Laplacian matrix, using tools provided by NetworkX and Numpy.
The Laplacian Matrix
This post will look at the basic, unnormalized Laplacian matrix of a graph - specifically, we will look at a 3-regular graph. We will see how to manually compute the laplacian from the Degree Matrix and the Adjacency Matrix of a graph, and will subsequently see how to derive the Laplacian Matrix with a networkx function.
Uploading Files with Django Ninja
This short post will demonstrate how to upload files, using the django-ninja API library.
Treemaps in Python with Football Data
In this post, we scrape data from the English Premier League 2020/21, and plot the goals scored by each team as a treemap visualization in Python. We use the Squarify library, in concert with Matplotlib, to plot and customize treemaps. Finally, we show an alternative visualization for the goals scored - a horizontal barplot.
Modelling Euro 2020 Results with Python Dataclasses #2
This post follows from the previous post, where we model Euro 2020 data scraped from the BBC website as Python dataclasses. In this post, we'll create a TeamStats class to model a team's tournament statistics, such as the number of goals they scored and conceded, the number of games won, drawn and lost, and the number of games that went to penalties.
Modelling Results with Python Dataclasses
In this post, we'll look at how to model the data scraped in the "Scraping Football Data with Beautiful Soup In Python" post using Python's dataclasses. We'll create a Result class that can encapsulate information about the team names and goals scored in a given match at the Euro 2020 Championships.
Setting up SQLModel
In this second part of our music track API series, we will look at how to add the new SQLModel package to our application. Specifically, we look at how to set up a SQLite database using SQLModel, allowing us to create a table for our music tracks based on a type-hinted schema. We then modify our FastAPI startup event to load the track data into our database table.
Creating A Music Track API with FastAPI in Python
This post outlines how to use the basic features of FastAPI to create a music tracks CRUD API. We create endpoints that allow us to get music tracks, get a single music track, as well as create, update and delete tracks. This is the first post in a short series, where we delve into the FastAPI ecosystem - in the next post, we'll adapt the API to use a relational database and use the shiny new SQLModel package!
Scraping Football Data with Beautiful Soup In Python
In this post, we will look at how to scrape the football/soccer results from the Euro 2021 Championships, using the BeautifulSoup web scraping library for Python.
Refactor Clinic #3
In this post, we will demonstrate how to refactor code that searches for a given dictionary within a list of dictionaries. By cleaning up the code, we will highlight different constructs available in Python, to help developers get a better understanding of the overall language, and understand where certain features can be applied.
Refactor Clinic #2
In this post, we will demonstrate how to refactor code that extracts the values of attributes on a Python object (class) to a dictionary with the same key names. By cleaning up the code, we will highlight different constructs available in Python, to help developers get a better understanding of the overall language, and understand where certain features can be applied.
Refactor Clinic #1
In this post, we will demonstrate how to refactor a Python function to perform the same functionality, but in a cleaner and more concise manner. By cleaning up the code, we will highlight different constructs available in Python, to help developers get a better understanding of the overall language, and understand where certain features can be applied.
OpenStreetMap, Folium and GeoPy for Mapping and Distance Finding
In this tutorial, we will look at how to convert ZIP-codes to latitude and longitude coordinates using an OpenStreetMap API lookup, and will learn how to plot these coordinates on a Leaflet map using Python's excellent Folium library. We will also find out how to calculate the distance between two geographical points, using the GeoPy library, and will look at how to find the mid-point between two points with some Python code.
Python Ternary Expressions
This post will outline Python's equivalent to the ternary expressions found in other languages such as C, Java and JavaScript. In Python, conditional expressions allow ternary-like logic to be expressed, giving programmers the ability to define single-line conditional logic.
Lambda Functions in Python
This post introduces Lambda Functions in Python, and demonstrates their usage with a few examples. Firstly, we create a lambda to represent Einstein's famous equation, E = mc^2. Secondly, we create a lambda function to calculate a company's corporation tax bill based on their income and expenses. And finally, we demonstrate a common use of lambda functions - as parameters to other Python functions such as sorted(), min() and max().
Django-Ninja - Creating a CRUD API
The second part of the django-ninja series - we look at extending our Music Track API to a full CRUD API, by creating an endpoint for creating music tracks (POST), changing existing music tracks (PUT) and deleting existing music tracks (DELETE).
List Comprehensions
This conceptual post will demonstrate how to use list comprehensions to cut down on code, and express iterative logic in a concise, Pythonic manner. We look at examples involving lists of numbers and lists of strings (berries!), and show how to achieve results using list comprehensions.
Django-Ninja - An Introduction
In this post, we create a few endpoints Music Track API using django-ninja - a nice alternative to Django REST Framework that is heavily inspired by the FastAPI web framework. We show how easy it is to wire up API routes and retrieve (and serialize) data using the Django ORM and django-ninja Schema objects.
Django-Unicorn - An Introduction
This post will demonstrate some of the features and concepts offered by the django-unicorn library, an interactive library for creating rich components in Django, which minimizes the JavaScript footprint involved in creating dynamic user interface components. We will build a movie list component, that allows the user to dynamically add to a list of movies and delete movies from their templates - all without writing any AJAX calls.
Application Configuration with django-environ
This post will demonstrate some best practices in configuring Django applications. We will look at the 12 Factor approach for configuration, and will learn about the django-environ package that helps to secure applications using environment variables, safe defaults, and type-casting.
Django - Using a Custom User Model
This post will outline different approaches for customizing Django's default User model, and will demonstrate how to extend this model with new fields, and how to remove existing User model fields that you do not need. We will pay close attention to two abstract classes in Django - AbstractUser and AbstractBaseUser - that both enable flexible extensions of Django's User system.
Django SES
This post will demonstrate how to integrate Amazon SES with Django applications, for sending emails. You will learn how to configure SES, how to use the django-ses library, and how to send test emails from Django to ensure your setup works as expected.