Skip to content

urls

URL manipulation and validation helpers.

URL_REGEX = "^(?:http(s)?:\\/\\/)[\\w.-]+(?:\\.[\\w\\.-]+)+[-\\w\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$" module-attribute

Regular expression matching absolute http/https URLs with a hostname.

check_invalid_external_url(url)

Checks if a URL is a valid external URL. If it is not, it will return an error message. If it is, it will return an empty string.

Parameters:

Name Type Description Default
url str

The URL to check

required

Returns:

Type Description
str

An error message if the URL is invalid, otherwise an empty string

friendly_urls(url)

Converts a URL to a friendly URL. This removes the leading slash and converts "index" to "/"

Parameters:

Name Type Description Default
url str

The URL to convert

required

Returns:

Type Description
str

The friendly URL

is_data_url(url)

Checks if a URL is a data URL.

Parameters:

Name Type Description Default
url str

The URL to check

required

Returns:

Type Description
bool

True if the URL is a data URL, False otherwise

is_external_url(url)

Checks if a URL is an external URL.

Parameters:

Name Type Description Default
url str

The URL to check

required

Returns:

Type Description
bool

True if the URL is external, False otherwise

is_valid_url(url)

Checks if a URL is a valid URL.

Parameters:

Name Type Description Default
url str

The URL to check

required

Returns:

Type Description
bool

True if the URL is valid, False otherwise

merge_url_query_params(url, additional_params)

Merges additional parameters into a URL. If a parameter already exists, it will be overwritten. For more information, see: https://stackoverflow.com/a/52373377

Parameters:

Name Type Description Default
url str

The URL to merge the parameters into

required
additional_params dict

The parameters to merge into the URL.

required

Returns:

Type Description
str

The URL with the additional parameters

remove_url_query_params(url, params_to_remove)

Removes parameters from a URL. If a parameter does not exist, it will be ignored.

Parameters:

Name Type Description Default
url str

The URL to remove the parameters from

required
params_to_remove set

The parameters to remove from the URL

required

Returns:

Type Description
str

The URL with the parameters removed