Python Flask : Starter Automated Template

When starting new project again, Some common structure needed to be written again and again. So I wrote a program nearly three years (2021) ago to Get Everything ready on one script. Here is the python script that makes everything ready

Here is is the revised version of the code

The Program Automatically generates basic scripts and files and necessary folder.

Copy, run the script And adjust the script on your necessary.


import os

x = os.getcwd()

os.mkdir("templates")
os.mkdir(os.path.join(x, "templates", "layouts"))
os.mkdir("static")
os.mkdir(os.path.join(x, "static", "css"))
os.mkdir(os.path.join(x, "static", "js"))
os.mkdir(os.path.join(x, "static", "img"))

app_w = """from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def home():
	title = "Home"
	return render_template('index.html', title=title)
"""

with open("app.py", "w+") as f:
	f.write(app_w)

css = """
body {
	background-color: #ccebff;
}
a, h1, h2, p {
	color: #3377ba8;
}
"""

with open(os.path.join(x, "static", "css", "main.css"), "w+") as f:
	f.write(css)

main = """<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ title }}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
</head>
<body>
{% block content %}
{% endblock %}
<footer>
<ul>
	<li> <a href="/">Home</a></li>
</ul>
&copy; 2021 Mahid
</footer>
</body>
</html>
"""

with open(os.path.join(x, "templates", "layouts", "main.html"), "w+") as f:
	f.write(main)

index = """{% extends "layouts/main.html" %}
{% block content %}
<h1>{{ title }}</h1>
<p>Hello world</p>
{% endblock %}
"""

with open(os.path.join(x, "templates", "index.html"), "w+") as f:
	f.write(index)
    
    
    

Related Posts

Flask session simplest example
July 3, 2024

Flask sessions are useful when we want to remember a specific user. For example to know the user already visited the website before. So we can generate dynamic content for that user. Or we can use login based system In this code we will use the simplest flask session example Run the code go the […]

Expiring Links Tutorial: Python Flask One-Time Link Generator
June 15, 2024

One time links are essential where users want to share resources with co-worker or friend’s effortlessly but at the same time restrict other from access. Its works best because the link automatically inactive if some one go to the link for the first time. That means the same link will not work for the second […]

Create your first chat application with Flask and Socketio
April 27, 2024

We will use socketio for real-time messaging First install our dependency for this application pip install flask pip install flask-socketio Copy Code How it will work. Every user will get an unique key. If the the other user submit the other users key that will send the message. its like sending message to unique username […]