DataURI
=======
.. image:: https://github.com/fcurella/python-datauri/workflows/Python%20Tests/badge.svg
:target: https://github.com/fcurella/python-datauri/actions?query=workflow%3A%22Python+Tests%22
:alt: Build status of the master branch on Mac/Linux
.. image:: https://coveralls.io/repos/github/fcurella/python-datauri/badge.svg?branch=master
:target: https://coveralls.io/github/fcurella/python-datauri?branch=master
Data URI manipulation made easy.
This isn't very robust, and will reject a number of valid data URIs. However, it meets the most useful case: a mimetype, a charset, and the base64 flag.
Installation
------------
.. code-block:: bash
$ pip install python-datauri
Parsing
-------
.. code-block:: python
>>> from datauri import DataURI
>>> uri = DataURI('data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu')
>>> uri.mimetype
'text/plain'
>>> uri.charset
'utf-8'
>>> uri.is_base64
True
>>> uri.data
b'The quick brown fox jumped over the lazy dog.'
Note that ``DataURI.data`` will always return bytes.
Use ``DataURI.text`` to get a string.
Creating from a string
----------------------
.. code-block:: python
>>> from datauri import DataURI
>>> made = DataURI.make('text/plain', charset='us-ascii', base64=True, data='This is a message.')
>>> made
DataURI('data:text/plain;charset=us-ascii;base64,VGhpcyBpcyBhIG1lc3NhZ2Uu')
>>> made.data
b'This is a message.'
Creating from a file
--------------------
This is really just a convenience method.
.. code-block:: python
>>> from datauri import DataURI
>>> png_uri = DataURI.from_file('somefile.png')
>>> png_uri.mimetype
'image/png'
>>> png_uri.data
b'\x89PNG\r\n...'
Serializing
-----------
`DataURI` is a subclass of `str`, so you can just use `str()` to print it out:
.. code-block:: python
>>> from datauri import DataURI
>>> png_uri = DataURI.from_file('somefile.png')
>>> str(png_uri)
'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIA...'
Pydantic Support
----------------
You can use `DataURI` as a type for `Pydantic` v1:
.. code-block:: python
from datauri import DataURI
from pydantic import BaseModel
class ProfilePicture(BaseModel):
image_data: DataURI
License
-------
This code is released under the `Unlicense <http://unlicense.org/>`_.
Credits
-------
This is a repackaging of `this Gist <https://gist.github.com/zacharyvoase/5538178>`_
originally written by `Zachary Voase <https://github.com/zacharyvoase>`_.
Raw data
{
"_id": null,
"home_page": "https://github.com/fcurella/python-datauri/",
"name": "python-datauri",
"maintainer": "Flavio Curella",
"docs_url": null,
"requires_python": null,
"maintainer_email": "flavio.curella@gmail.com",
"keywords": null,
"author": null,
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/74/a6/f26405673d841208af6eb80f161638a55597f1d25569ccbe1bfb52f9fd08/python_datauri-2.1.1.tar.gz",
"platform": "any",
"description": "DataURI\n=======\n\n.. image:: https://github.com/fcurella/python-datauri/workflows/Python%20Tests/badge.svg\n :target: https://github.com/fcurella/python-datauri/actions?query=workflow%3A%22Python+Tests%22\n :alt: Build status of the master branch on Mac/Linux\n\n.. image:: https://coveralls.io/repos/github/fcurella/python-datauri/badge.svg?branch=master\n :target: https://coveralls.io/github/fcurella/python-datauri?branch=master\n\nData URI manipulation made easy.\n\nThis isn't very robust, and will reject a number of valid data URIs. However, it meets the most useful case: a mimetype, a charset, and the base64 flag.\n\n\nInstallation\n------------\n\n.. code-block:: bash\n\n $ pip install python-datauri\n\n\nParsing\n-------\n\n.. code-block:: python\n\n >>> from datauri import DataURI\n >>> uri = DataURI('data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu')\n >>> uri.mimetype\n 'text/plain'\n >>> uri.charset\n 'utf-8'\n >>> uri.is_base64\n True\n >>> uri.data\n b'The quick brown fox jumped over the lazy dog.'\n\nNote that ``DataURI.data`` will always return bytes.\nUse ``DataURI.text`` to get a string.\n\nCreating from a string\n----------------------\n\n.. code-block:: python\n\n >>> from datauri import DataURI\n >>> made = DataURI.make('text/plain', charset='us-ascii', base64=True, data='This is a message.')\n >>> made\n DataURI('data:text/plain;charset=us-ascii;base64,VGhpcyBpcyBhIG1lc3NhZ2Uu')\n >>> made.data\n b'This is a message.'\n\nCreating from a file\n--------------------\n\nThis is really just a convenience method.\n\n.. code-block:: python\n\n >>> from datauri import DataURI\n >>> png_uri = DataURI.from_file('somefile.png')\n >>> png_uri.mimetype\n 'image/png'\n >>> png_uri.data\n b'\\x89PNG\\r\\n...'\n\n\nSerializing\n-----------\n\n`DataURI` is a subclass of `str`, so you can just use `str()` to print it out:\n\n.. code-block:: python\n\n >>> from datauri import DataURI\n >>> png_uri = DataURI.from_file('somefile.png')\n >>> str(png_uri)\n 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIA...'\n \nPydantic Support\n----------------\n\nYou can use `DataURI` as a type for `Pydantic` v1:\n\n.. code-block:: python\n\n from datauri import DataURI\n from pydantic import BaseModel\n\n\n class ProfilePicture(BaseModel):\n image_data: DataURI\n\nLicense\n-------\n\nThis code is released under the `Unlicense <http://unlicense.org/>`_.\n\nCredits\n-------\n\nThis is a repackaging of `this Gist <https://gist.github.com/zacharyvoase/5538178>`_\noriginally written by `Zachary Voase <https://github.com/zacharyvoase>`_.\n",
"bugtrack_url": null,
"license": "Unlicense",
"summary": "A li'l class for data URI manipulation in Python",
"version": "2.1.1",
"project_urls": {
"Homepage": "https://github.com/fcurella/python-datauri/"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "61c12c369dbbf2d05582a2b78c59bdc41d80e3837c04b2c426a94e387e47308e",
"md5": "10304170aaafa540cb409eb66603988d",
"sha256": "a952f1dba9103631536b0c01ca946f5a0afecb32e4a4e8ea6029c7f250073389"
},
"downloads": -1,
"filename": "python_datauri-2.1.1-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "10304170aaafa540cb409eb66603988d",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 5557,
"upload_time": "2024-05-15T01:28:09",
"upload_time_iso_8601": "2024-05-15T01:28:09.594187Z",
"url": "https://files.pythonhosted.org/packages/61/c1/2c369dbbf2d05582a2b78c59bdc41d80e3837c04b2c426a94e387e47308e/python_datauri-2.1.1-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "74a6f26405673d841208af6eb80f161638a55597f1d25569ccbe1bfb52f9fd08",
"md5": "34210bdd61cc932fa52e61c94750c380",
"sha256": "842614009a2347cd7bf593aff124f371671eff6df06ab14bbdf643f230c2bfd9"
},
"downloads": -1,
"filename": "python_datauri-2.1.1.tar.gz",
"has_sig": false,
"md5_digest": "34210bdd61cc932fa52e61c94750c380",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 8290,
"upload_time": "2024-05-15T01:28:10",
"upload_time_iso_8601": "2024-05-15T01:28:10.739240Z",
"url": "https://files.pythonhosted.org/packages/74/a6/f26405673d841208af6eb80f161638a55597f1d25569ccbe1bfb52f9fd08/python_datauri-2.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-15 01:28:10",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "fcurella",
"github_project": "python-datauri",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [],
"lcname": "python-datauri"
}