Skip to content

Instantly share code, notes, and snippets.

@joreilly86
Created October 23, 2024 23:47
Show Gist options
  • Select an option

  • Save joreilly86/0144f7aa8f2e4a5fa2030bac2896e91b to your computer and use it in GitHub Desktop.

Select an option

Save joreilly86/0144f7aa8f2e4a5fa2030bac2896e91b to your computer and use it in GitHub Desktop.
A simple Python project setup template using Cookiecutter to automate directory structure creation and environment management. This template includes folders for data and source code, a Jupyter notebook, and integrates with uv for easy virtual environment setup and package management. Streamline your workflow and standardize your project setup w…
import json
import os
import shutil
import subprocess
import sys
from cookiecutter.main import cookiecutter
# Template Directory Structure
template = {
"cookiecutter.json": json.dumps({"project_name": "default_project_name"}),
"{{cookiecutter.project_name}}": {
"data": {}, # Data directory
"src": {}, # Source code directory
"notebook_01.ipynb": "", # Jupyter notebook file
"README.md": "# {{cookiecutter.project_name}}\n\nA Jupyter notebook-based project.",
},
}
def create_template_structure(base_path, structure):
for name, content in structure.items():
path = os.path.join(base_path, name)
if isinstance(content, dict):
os.makedirs(path, exist_ok=True)
create_template_structure(path, content)
else:
with open(path, "w") as file:
file.write(content)
def run_command(command, cwd=None):
try:
subprocess.run(command, check=True, cwd=cwd)
except subprocess.CalledProcessError as e:
print(f"Error running command {' '.join(command)}: {e}")
sys.exit(1)
def initialize_uv_project(project_path):
if not shutil.which("uv"):
print("Error: 'uv' is not installed or not in the system PATH.")
sys.exit(1)
run_command(["uv", "init"], cwd=project_path)
# After initializing, add ipykernel which also creates the venv
run_command(["uv", "add", "ipykernel"], cwd=project_path)
def main():
base_dir = os.path.abspath("./cookiecutter-template")
create_template_structure(base_dir, template)
print(f"Template created at: {base_dir}")
try:
project_dir = cookiecutter(base_dir)
initialize_uv_project(project_dir)
except Exception as e:
print(f"An error occurred: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
@joreilly86
Copy link
Author

Instructions

  1. Clone or download the repository: Place the files in the root folder where you want to set up your new project.

  2. Install dependencies:

    • Make sure you have cookiecutter and uv installed.
    • If not, you can install them via pip:
      pip install cookiecutter uv
  3. Run the script:
    Navigate to the directory where you placed the files and run the script:

    python create_notebook_project.py
  4. Customize the project name: The script will prompt you for a project name. Provide your preferred name, and it will generate the project structure based on the template.

  5. Environment Setup: Once the project structure is created, the script will initialize a uv virtual environment and set up Jupyter notebook integration automatically.

  6. Start working: Your project is now ready! Navigate to the generated project directory and start adding your code and data.

Fore more Python for Engineering content, check out flocode.substack.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment