Skip to main content

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

Project description

python-js-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.

This is an example of a JavaScript data file:

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

Think of this module as the JSON module, but for JavaScript files.

Usage

  • You can read a JavaScript file with read_js(path, varname)
  • You can write a JavaScript file with write_js(path, value, varname)
  • You can append an item to a JavaScript array with append_to_js_array(path, value)
  • You can append a key-value pair to a JavaScript object with append_to_js_object(path, key, value)

Installation

Copy the file src/javascript folder into your project. You probably want to copy the tests as well.

Eventually this will be a pip installable package, once I've done some more testing (see issue #3).

Why not use JSON files?

If you've opening an HTML file from disk, you can load data from a local JavaScript file, for example:

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

This is the only way to load data from an external file from an HTML file you've opened locally -- you can't do this with a JSON file, for example.

I have a lot of HTML files and local sites I build with an HTML viewer and metadata in a JavaScript file. The convenience of this approach outweighs the mild annoyance of having to store data in JavaScript, not JSON.

License

MIT.

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

alexwlchan_python_js_files-0.0.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file alexwlchan_python_js_files-0.0.1.tar.gz.

File metadata

File hashes

Hashes for alexwlchan_python_js_files-0.0.1.tar.gz
Algorithm Hash digest
SHA256 1e90cbd38b35d86e1e855efad4dc99c3708a1e15fc5b4bcc31b204d92695afc0
MD5 205fb45b324877958c426572c5dd8f87
BLAKE2b-256 43aad9f0b02152e2e3e8c2fb5eee75df4bbec13adcfb2a21ec73791c69d0c15d

See more details on using hashes here.

File details

Details for the file alexwlchan_python_js_files-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for alexwlchan_python_js_files-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1e68cf8ba5b419298978573ae5657c8e6c27d80c5aa9acb1ed423450d3d62594
MD5 067b9ce84daf2ba80132150903199443
BLAKE2b-256 881bd6a1aa08f70d3d507155bb6fbf85b57b6e07e548bc595bff23c7f7379f78

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