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:
-
Copy the
src/javascriptdirectory and its tests directly into your project. Just keep a link back here! -
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.
Metadata
Release files for javascript_data_files 1.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| javascript_data_files-1.5.0.tar.gz | 15.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| javascript_data_files-1.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.4 kB
Release files / javascript_data_files-1.5.0.tar.gz
| Download URL | javascript_data_files-1.5.0.tar.gz |
|---|---|
| Size | 15.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1dcbec78164b022aca7f4b7fcd271bb6f0020d2c00fc1f2592687b42a5c2bf80
|
|
BLAKE2b-256 checksum How to use checksums |
970e144ba9f7e4baefe40474928195ff57580c1c1850fd56c8b5f3c56714da57
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / javascript_data_files-1.5.0-py3-none-any.whl
| Download URL | javascript_data_files-1.5.0-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0ce2da9e62268e4003fa843b42ee911e50d5a17de480a5d0b34364698349ef9e
|
|
BLAKE2b-256 checksum How to use checksums |
8caa682e24dd5a8ebb3a944e14d11229512f26a783e18be9ecc5fc5c3ef6cd2d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|