python-datauri


Namepython-datauri JSON
Version 3.0.1 PyPI version JSON
download
home_pagehttps://github.com/fcurella/python-datauri/
SummaryA li'l class for data URI manipulation in Python
upload_time2024-11-27 17:45:07
maintainerFlavio Curella
docs_urlNone
authorNone
requires_pythonNone
licenseUnlicense
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            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`:

.. 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/5c/98/b689dddfd8a1eb0494cdfa9df6fce147e73fbab3f817140a022bee49091c/python_datauri-3.0.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`:\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": "3.0.1",
    "project_urls": {
        "Homepage": "https://github.com/fcurella/python-datauri/"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fb0d84a7b28ea602468068ffac00cb335f7868ba8b8d5af1482ec4d207492413",
                "md5": "fd2af02ebcc37e56414bb5830b72abe0",
                "sha256": "36c6cdd047329237f8d147843b8ae6749a0d9864ac60b9ad883101ff2cce4f0b"
            },
            "downloads": -1,
            "filename": "python_datauri-3.0.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fd2af02ebcc37e56414bb5830b72abe0",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 5731,
            "upload_time": "2024-11-27T17:45:06",
            "upload_time_iso_8601": "2024-11-27T17:45:06.430651Z",
            "url": "https://files.pythonhosted.org/packages/fb/0d/84a7b28ea602468068ffac00cb335f7868ba8b8d5af1482ec4d207492413/python_datauri-3.0.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5c98b689dddfd8a1eb0494cdfa9df6fce147e73fbab3f817140a022bee49091c",
                "md5": "e5d0495224fed83ca383a031194b3ecd",
                "sha256": "822661c77473507dc35b57957c5c979f0f005927509153dcc04760df11f1e586"
            },
            "downloads": -1,
            "filename": "python_datauri-3.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "e5d0495224fed83ca383a031194b3ecd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9344,
            "upload_time": "2024-11-27T17:45:07",
            "upload_time_iso_8601": "2024-11-27T17:45:07.920588Z",
            "url": "https://files.pythonhosted.org/packages/5c/98/b689dddfd8a1eb0494cdfa9df6fce147e73fbab3f817140a022bee49091c/python_datauri-3.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-27 17:45:07",
    "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"
}
        
Elapsed time: 1.08048s