Skip to main content

Work with JSON which is stored as a value in a JavaScript file

Project description

javascript-data-files

This is a collection of Python functions for manipulating JavaScript "data files" -- that is, JavaScript files that define a single variable with a JSON value.

Think of this as the Python json module, but built specifically for JavaScript files like this:

const shape = {"sides": 5, "colour": "red"};

Why not use JSON files?

If you open a local HTML file directly from your disk (file://...), browsers block you from loading local .json files via fetch() or XMLHttpRequest due to CORS (Cross-Origin Resource Sharing) restrictions.

However, you can load a local JavaScript file using a standard script tag:

<script src="file:///Users/alexwlchan/repos/javascript-data-files/data.js"></script>

I build and maintain a lot of local static websites and offline HTML viewers. Storing my metadata in JavaScript files rather than JSON is the only way to load external data into these local files without the hassle of running a local web server.

I have a lot of local static websites that I use for my local media archives. Storing my metadata in JavaScript files rather than JSON allows me to load metadata in an HTML file without the hassle of running a local web server.

The minor annoyance of writing to .js files instead of pure .json is easily offset by the convenience of just double-clicking an HTML file to open it. This library bridges the gap, letting my Python backend scripts easily read, write, and update those JavaScript data files.

Usage

from javascript_data_files import (
    read_js,
    write_js,
    append_to_js_array,
    append_to_js_object,
    read_typed_js,
)

The library provides four core functions:

  • Read: Get a file's value with read_js(path, varname)
  • Write: Create or overwrite a file with write_js(path, value, varname)
  • Append to array: Add an item to a JavaScript array with append_to_js_array(path, value)
  • Append to object: Add a key-value pair to a JavaScript object with append_to_js_object(path, key, value)

If you install the typed extra, you can validate your JavaScript data against a Python type or Pydantic model:

  • Typed read: read_typed_js(path, varname, model)

Installation

You have two options:

  1. Copy the src/javascript directory and its tests directly into your project. Just keep a link back here!

  2. Install via pip:

    $ pip install javascript-data-files
    

    To include type-checking capabilities:

    $ pip install javascript-data-files[typed]
    

Versioning and stability

This library uses semantic versioning. Patch and minor versions will not contain breaking changes.

In practice, the library is unlikely to change much in future versions – it does one specific job, and it does it well.

Development and feedback

Want to contribute or tweak something? Check out CONTRIBUTING.md for local setup instructions.

License

This project is shared under the MIT license.

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

javascript_data_files-1.5.0.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

javascript_data_files-1.5.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file javascript_data_files-1.5.0.tar.gz.

File metadata

  • Download URL: javascript_data_files-1.5.0.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for javascript_data_files-1.5.0.tar.gz
Algorithm Hash digest
SHA256 1dcbec78164b022aca7f4b7fcd271bb6f0020d2c00fc1f2592687b42a5c2bf80
MD5 2b4406673d2c442bbeb53ab11b48c3d3
BLAKE2b-256 970e144ba9f7e4baefe40474928195ff57580c1c1850fd56c8b5f3c56714da57

See more details on using hashes here.

File details

Details for the file javascript_data_files-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: javascript_data_files-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for javascript_data_files-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ce2da9e62268e4003fa843b42ee911e50d5a17de480a5d0b34364698349ef9e
MD5 66e2a562199b6a585201567c5232c78e
BLAKE2b-256 8caa682e24dd5a8ebb3a944e14d11229512f26a783e18be9ecc5fc5c3ef6cd2d

See more details on using hashes here.

Supported by

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