Skip to content

Instantly share code, notes, and snippets.

@sudodo
Created October 13, 2023 09:26
Show Gist options
  • Save sudodo/5eb1833ce04281f52f005e592676d208 to your computer and use it in GitHub Desktop.
Save sudodo/5eb1833ce04281f52f005e592676d208 to your computer and use it in GitHub Desktop.
Create new blank Flask app files.
import os
# Define the directory and file structure
structure = {
'myflaskapp': {
'app': {
'templates': {},
'static': {
'css': {},
'js': {},
'images': {}
},
'blueprints': {
'__init__.py': ''
},
'__init__.py': '',
'routes.py': '',
'models.py': ''
},
'migrations': {},
'tests': {
'__init__.py': '',
'test_routes.py': ''
},
'config.py': '',
'run.py': '',
}
}
# Function to create the directory and file structure
def create_structure(base, structure):
for name, contents in structure.items():
path = os.path.join(base, name)
if isinstance(contents, dict):
os.makedirs(path, exist_ok=True)
create_structure(path, contents)
else:
with open(path, 'w') as f:
f.write(contents)
# Write the desired code into app.py
code = """# app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "Hello, World!"
"""
# Add the code to the structure
structure['myflaskapp']['app']['app.py'] = code
# Create the structure in the current working directory
create_structure(os.getcwd(), structure)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment