JSBeautifier Python Library

1.15.4 · active · verified Thu Apr 09

jsbeautifier is the official Python wrapper for the popular js-beautify project, providing JavaScript, HTML, and CSS code formatting and beautification capabilities. It is actively maintained with releases typically aligning with the core JavaScript library, currently at version 1.15.4.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to beautify JavaScript and HTML code using `jsbeautifier`. It shows how to import the necessary functions, create and customize an options object, and apply it to format code strings.

import jsbeautifier

# Example for JavaScript beautification
js_code = "function test( ){ var x=1;if(x){ return x;} else { return 0;}}"

# Create and customize options object
options = jsbeautifier.default_options()
options.indent_size = 2
options.space_in_paren = True
options.keep_array_indentation = True

beautified_js = jsbeautifier.beautify(js_code, options)

print("Original JS:\n", js_code)
print("\nBeautified JS:\n", beautified_js)

# Example for HTML beautification (using the same options)
html_code = "<html><head><title>Test</title></head><body><h1>Hello</h1>  <p>World</p></body></html>"
beautified_html = jsbeautifier.html_beautify(html_code, options)

print("\nOriginal HTML:\n", html_code)
print("\nBeautified HTML:\n", beautified_html)

view raw JSON →