data_url Documentation
Usage
Creating a data URL
If all you need is to create a URL and nothing else there is a shortcut method included so you don’t need to directly instantiate the DataURL class.
import data_url
with open('image.jpeg', 'rb') as image:
data = image.read()
url = data_url.construct_data_url(
mime_type='image/jpeg',
base64_encoded=True,
data=data,
)
If you need the information to persist it is recommended to instantiate a class through one of the factory methods on DataURL
import data_url
with open('image.jpeg', 'rb') as image:
data = image.read()
url = data_url.DataURL.from_data('image/jpeg', True, data)
print(str(url))
You can access the full data URL by either converting the DataURL object to a string as above or by accessing the url attribute.
Retrieving data from a URL
Given you already have a data URL you can instantiate a DataURL object and retrieve each individual attribute.
import data_url
raw_url = "data:application/json;base64,ewogICJ0ZXN0IjogMTIzCn0K"
url = data_url.DataURL.from_url(raw_url)
print(url.mime_type, url.is_base64_encoded, url.data)
API Documentation
- class data_url.DataURL
- ENCODING_STRING = ';base64'
- URL_FORMAT = 'data:{mime_type}{parameters}{encoded},{data}'
- property data: str | bytes
The raw data of the URL
- property encoded_data: str
The encoded data of the URL
- classmethod from_byte_data(mime_type: str, data: bytes) DataURL
Create a new data URL from a mime type and byte data.
This method works similarly to from_data, however because the data is bytes type it will automatically turn on base64 encoding. It also assumes that the data is not already base64 encoded. If you have base64 encoded bytes convert them to a string then use the from_data method.
- Parameters:
mime_type (str)
data (bytes) – The actual url data.
- Returns:
A new DataURL object.
- Return type:
- classmethod from_data(mime_type: str, base64_encoded: bool, data: str) DataURL
Create a new data URL from a mime type and data
If the data is a string type and the base64_encoded flag is set to True then this function assumes the data is already base64 encoded and decodes it. Otherwise the data is passed through as is.
- Parameters:
mime_type (str)
base64_encoded (boolean) – Whether or not the URL data should be base64 encoded. If True the data passed to this method is also assumed to be base64 encoded.
data (str) – The actual url data.
- Returns:
A new DataURL object.
- Return type:
- classmethod from_url(url: str) DataURL | None
Create a new DataURL object from an existing URL. Useful for retrieving data from a data URL.
- Parameters:
url (str)
- Returns:
A new DataURL object.
- Return type:
- property is_base64_encoded: bool
Whether or not the data URL data is base64 encoded
- property mime_type: str
- property parameters: Dict[str, str]
Attribute / Value parameters.
- property url: str
- data_url.construct_data_url(mime_type: str, base64_encoded: bool, data: str | bytes) str
Helper method for just creating a data URL from some data. If this URL will persist it is recommended to create a full DataURL object
If the data is a string type and the base64_encode flag is set to True then this function assumes the data is already base64 encoded and decodes it. Otherwise the data is passed through as is.
- Parameters:
mime_type (str)
base64_encoded (boolean) – Whether or not the URL data should be base64 encoded. If True the data passed to this method is also assumed to be base64 encoded. This parameter is ignored when data is bytes type and data is assumed to not be encoded.
data (str | bytes) – The actual url data.
- Returns:
The data URL.
- Return type:
str