Skip to content

Instantly share code, notes, and snippets.

@jesshart
Last active October 29, 2024 20:53
Show Gist options
  • Save jesshart/6154ac55f6cb9a139ab202f336e13f34 to your computer and use it in GitHub Desktop.
Save jesshart/6154ac55f6cb9a139ab202f336e13f34 to your computer and use it in GitHub Desktop.
Hatch Party Tricks

Hatch Party Tricks

Setting Up

Add hatch autocomplete for oh-my-zsh

  1. Run this command
mkdir -p ${ZSH_CUSTOM:=~/.oh-my-zsh/custom}/plugins/hatch-zsh-completion && _HATCH_COMPLETE=zsh_source hatch > ${ZSH_CUSTOM}/plugins/hatch-zsh-completion/hatch-zsh-completion.plugin.zsh
  1. Add hatch-zsh-completion to your .zshrc file at plugins= like so
plugins=(git hatch-zsh-completion)

Using environments

Run a script from a non-default environment

hatch run <env-name>:<script-alias>

Deactivate an environment

exit

Enter an environment

hatch -e <env-name> shell

Adding environment variables

[tool.hatch.envs.<env-name>.env-vars]
NAME = "value"

Run with uv as the virtual env backend.
Link to discussion

Using Environment Variables in pyproject.toml with Hatch

When configuring Hatch to use environment variables in pyproject.toml, note that TOML doesn’t directly support variable substitution within strings. To work around this, we can leverage shell commands to access environment variables.

Here's an example setup to pull data from Kaggle, unzip it, and launch a notebook using custom paths:

[tool.hatch.envs.default.scripts]
pull-data = [
  "sh -c 'kaggle datasets download -d $KAGGLE_PATH -p src/demo_supply_chain/data/raw'",
  "unzip src/demo_supply_chain/data/raw/supply-chain-analysis.zip -d src/demo_supply_chain/data/raw",
]
notebook = ["marimo edit src/demo_supply_chain/notebooks/demo.py"]

[tool.hatch.envs.default.env-vars]
DATA_PATH = "src/demo_supply_chain/data/raw/supply_chain_data.csv"
KAGGLE_PATH = "fayez1/inventory-management"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment