Skip to main content

Collects a set of svg vector graphics into a single json file using data URIs

Project description

svg2json

Collects a set of SVG vector graphics into a single JSON file using data URIs

This project is currently in beta stage

TODO: Motivation:

  • lots of small files
  • -> lots of communication overhead
  • -> bad UX - some icons (in browser cache) are visible, some pop up later

Dependencies

Build and Install from Source

./pkg-build
sudo pip install --upgrade ./dist/svg2json-0.2.0-py3-none-any.whl

Install via pip

pip install svg2json

Usage

svg2json --help
svg2json -o iconset.json iconset/*.svg
svg2json good.svg bad.svg > status.json

Example Use-Case

A set of icons shall be collected and used by a web application, instead of loading each icon individually. The client then should use the cached icons from the JSON file.

This example setup and the provided JavaScript code are based on and inspired by the MusicDB Project.

Server Side

# What we start with:
ls ./icons
# Add.svg  Album.svg  AlbumFile.svg ... vMax.svg  vMin.svg  vThis.svg
find ./icons -type f -name "*.svg" -print | wc -l
# 92 svg files
du -hc ./icons/*.svg
# 592KB of data

# Collecting all vector graphics into a singe JSON file:
svg2json -o icons.json ./icons/*.svg

# The resulting JSON file
du -h icons.json
# 204K - thanks to size optimization using Scour

Example of a generated JSON file (here: icons.json):

{
    "Add":       "data:image/svg+xml;base64,<data>",
    "Album":     "data:image/svg+xml;base64,<data>",
    "AlbumFile": "data:image/svg+xml;base64,<data>",
    // ...
    "vMax":      "data:image/svg+xml;base64,<data>",
    "vMin":      "data:image/svg+xml;base64,<data>",
    "vThis":     "data:image/svg+xml;base64,<data>"
}

Client side

The IconManager class loads the JSON file with all accumulated icons. Keep in mind that this happens asynchronously. So right after creating an instanced of this class, the cache is empty. Anyway the GetIcon code is aware of this situation and allows loading icons from the server that are not yet cached. Either the JSON file has not been loaded or the icons are not included in the cached icon files.

// Warning: I just hacked down this code for demonstration purpose.
// It certainly can be improved. At least by error handling inside the LoadIcons method.

class IconManager
{
    constructor(fileurl=null)
    {
        this.iconcache = new Object();  // Empty object = empty cache
        if(typeof fileurl === "string")
            this.LoadIcons(fileurl);    // Load icons
    }

    LoadIcons(fileurl)
    {
        let request = new XMLHttpRequest();
        request.addEventListener("load", 
            (event)=>
            {
                let jsonstring = event.target.responseText;
                this.iconcache = JSON.parse(jsonstring);
            },
            false);
        request.open("GET", fileurl);
        request.send();
    }

    GetIcon(iconname)
    {
        // If icon exists in the cache, use it.
        // Otherwise load it from the server.
        let uri;
        if(iconname in this.iconcache)
            uri = `url(${this.iconcache[iconname]})`;
        else
            uri = `url("img/icons/${iconname}.svg")`;

        // Create image element and set the icon as source
        let icon = new Image();
        icon.src = uri;
        return icon;
    }
}
let iconmanager = new IconManager("img/icons.json");

// ...

let icon_add = iconmanager.GetIcon("Add");
document.body.appendChild(icon_add);

The shown example code comes without any dependencies despite some classes that are provided by web browsers:

  • XMLHttpRequest for loading files from the server
  • Image for creating an HTML image element (<img>)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

svg2json-0.2.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

svg2json-0.2.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file svg2json-0.2.0.tar.gz.

File metadata

  • Download URL: svg2json-0.2.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.10.4

File hashes

Hashes for svg2json-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2e5eb310e1e927026096c5bb494e171bba75e2e0491dc09f79cce487ca0331bc
MD5 68f2c992ba3b1903beacaf6bdbe700cd
BLAKE2b-256 a2f8801d67ebaa1cea26ce8d139cb6a7aef5c3561bb12658316e8042ee8f5af4

See more details on using hashes here.

File details

Details for the file svg2json-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: svg2json-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.10.4

File hashes

Hashes for svg2json-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff64c44d078576b31f30c47fb56825073809842f148c211cc67806f460a65c8c
MD5 d636b76323eca73ffa8144f326f65854
BLAKE2b-256 f655315a5a17389c871298381cea2cd8d32497d15e0da4a833b1bfaf3a02aaf0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page