DEV Community

Victor Peter
Victor Peter

Posted on

Generate HTML, CSS, JS project using Python

Introduction

Hey peeps, I'll like to show you a simple Python code that generates a folder, puts in your Html, css and javascript into that folder and also adds a little boilerplate code that you can start with.

Requirements

You'll need to have Python3 installed on your PC if you want to test out this code.

Let's get started.

First of all you will need to create a folder with the .py extension. Example "htmlcssjsgen.py".

Inside the file, type:
First Image

Line 1 and 2 imports sys and os inbuilt module. We use os on line 6 to get path, current working directory, etc.

The sys is used on line 4, 6, 9, etc to get the second argument passed to python after the file name argument on command line. That second argument will serve as the folder name for your HTML, CSS and JavaScript files. You will get to know how it is used in a minute.

Line 13, 15 and 17 creates the HTML, CSS and JavaScript files into the folder with the name you passed as an argument during when you'll run this script.

Then, add this:
Second Image

This obviously writes html sample code into the index.html file previously created.

Final set of codes to type:
Third Image
This also writes sample css into the style.css file and writes sample javascript function into style.js file we previously created.

line 52, 53 and 54 closes out those files that we just wrote into.

Save and run. Youll run it this way "python3 .py ".

I saved my script as "scafoldHTMLCSSJS.py" and ran "python3 scafoldHTMLCSSJS.py myproject".

"myproject" is gotten from sys.argv[1].

The result will be:
myproject/index.html
myproject/style.css
myproject/script.js

Conclusion

This script should help you in times when you want to quickly create a HTML, CSS and JS project automatically.

Top comments (0)