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

You have two options:

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

  2. Install the package using pip:

    $ pip install alexwlchan-python-js-files
    

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

javascript_data_files-1.0.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

javascript_data_files-1.0.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: javascript_data_files-1.0.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for javascript_data_files-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a90fed4dfa7f9bdfcc3e2a39debcd3d18676049f709b45bd72693d3231dba1bb
MD5 fcade1fd7e94c84fd8aef8111a15ca9a
BLAKE2b-256 3517c7320219e9576c9ce82a428196d2bd8ddd3e82712ce867421d3e012dbb62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for javascript_data_files-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dbc187c83e8849a55ed4023ba539a1d87306f7a11990fc93525a57695286c5d5
MD5 f46eeef4aae18d2338648b42f93a0499
BLAKE2b-256 c79730489d2bcb61335926f84e72ad84a44ad49800dafcc42c7331dc253a8ef4

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