Posts tagged: kedro
All posts with the tag "kedro"
Kedro rich is a very new and unstable (it’s good, just not ready) plugin for kedro to make the command line prettier.
Install kedro rich #
There is no pypi package yet, but it’s on github. You can pip install it with the git url.
pip install git+https://github.com/datajoely/kedro-rich
Kedro run #
You can run your pipeline just as you normally would, except you get progress bars and pretty prints.
kedro run
Kedro catalog #
Listing out catalog entries from the command line now print out a nice pretty table.
kedro catalog list
Give it a star #
Go to the GitHub repo and give it a star, Joel deserves it.
I keep my nodes short and sweet. They do one thing and do it well. I turn almost every DataFrame transformation into its own node. It makes it must easier to pull catalog entries, than firing up the pipeline, running it, and starting a debugger. For this reason many of my nodes can be built from inline lambdas.
Examples #
Here are two examples, the first one lambda x: x is sometimes referred
to as an identity function. This is super common to use in the early
phases of a project. It lets you follow standard layering conventions,
without skipping a layer, overthinking if you should have the layer or
not, and leaves a good placholder to fill in later when you need it.
Many times I just want to get the data in as fast as possible, learn about it, then go back and tidy it up.
from kedro.pipeline import node
my_first_node = node(
func=lambda x: x,
inputs='raw_cars',
output='int_cars',
tags=['int',]
)
my_first_node = node(
func=lambda cars: cars[['mpg', 'cyl', 'disp',]].query('disp>200'),
inputs='raw_cars',
output='int_cars',
tags=['pri',]
)
Note: try not to take the idea of a one liner too far. If your one line function wraps several lines down it probably deserves to be a real function for readability and a good docstring.
As you work on your kedro projects you are bound to need to add more
dependencies to the project eventually. Kedro uses a fantastic command
pip-compile under the hood to ensure that everyone is on the same version of
packages at all times, and able to easily upgrade them. It might be a bit
different workflow than what you have seen, let’s take a look at it.
git-status">git status #
Before you start mucking around with any changes to dependencies make sure that your git status is clean. I’d even reccomend starting a new branch for this, and if you are working on a team potentially submit this as its own PR for clarity.
git status
git checkout main
git checkout -b add-rich-dependency
requirements.in #
New requirements get added to a requirements.in file. If you need to specify an exact version, or a minimum version you can do that, but if all versions generally work you can leave it open.
# requirements.in
rich
Here I added the popular rich package to my requirements.in file. Since
I am ok with the latest version I am not going to pin anything, I am going to
let the pip resolver pick the latest version that does not conflict with any of
my dependencies for me.
build-reqs #
The command kedro build-reqs will tell kedro to recompile the
requirements.txt file that has all of our dependencies pinned down to exact
versions. This ensures that all of our teammates and production workflows use
the same exact versions of packages even if new ones are released after we
installed on our development machines.
kedro build-reqs
git add #
Now that we have our new dependencies ready to go commit those to git, and submit a PR for them if you are working on a team. This is a good way to document the discussion of adding new dependencies to your teams project.
git add requirements.in
git add requirements.txt
git status
git commit -m "FEAT updated dependencies with rich"
git push
# go make a pr
gh pr create --title "feat add rich to dependencies" --body "I added rich as a dependency, and ran pip-compile"
I am a huge believer in practicing your craft. Professional athletes spend most of their time honing their skills and making themsleves better. In Engineering many spend nearly 0 time practicing. I am not saying that you need to spend all your free time practicing, but a few minutes trying new things can go a long way in how you understand what you are doing and make a hue impact on your long term productivity.
Start practicing #
practice building pipelines with #kedro today
Go to your playground directory, and if you don’t have one, make one.
cd ~/playground
get pipx #
Install pipx in your system python. This is one of the very few, and possibly the only python library that deserves to be installed in your system directory, primarily because its used to sanbox clis in their own virtual environment automatically for you.
pip install pipx
make a new project #
From inside your playground directory, start your new kedro project.
This is quite simple and painless. So much so that if you mess this one
up doing something wild, it might be easier to make a new one that
fixing the wild one.
pipx run kedro new
# answer the questions it asks
I use this quite often to try out new things in a safe place.
Make a virtual environment #
Using Conda #
Conda is a fine choice to manage your virtual environments. It used to make things so much easier on windows that it was almost required. Nowadays getting python running on windows has become so much easier that this is less so.
conda create -n my-project python=3.8 -y
conda activate my-project
python -m pip install --upgrade pip
pip install -e src
one great benefit of conda is that it lets you choose the interpreter to go with your virtual environment.
Your new environment will be listed in your list of conda env here.
conda info --envs
Using venv #
venv is what I use now. Nothing against conda, it works great.
venv just feels a bit lighter and more common. I’ve actually grown to
appreciate that the venv is right where I put it, most often in the
project directory.
python -m venv .venv
source ./.venv/bin/activate
python -m pip install --upgrade pip
pip install -e src
using pipenv #
pipenv is another fine choice. I like how in one command it makes the
environment and activates it for you. pipenv also puts virtual
environments in the global directory.
pipx run pipenv shell
python -m pip install --upgrade pip
pip install -e src
Make pipelines #
Now go make some pipelines with your new project, try something wild, break it, and make another.
I just installed a brand new Ubuntu 21.10 Impish Indri, and wanted a kedro project to play with so I did what any good kedroid would do, I went to my command line and ran
pipx run kedro new --starter spaceflights
But what I got back was not what I expected!
Fatal error from pip prevented installation. Full pip output in file:
/home/walkers/.local/pipx/logs/cmd_2022-01-01_20.42.16_pip_errors.log
Some possibly relevant errors from pip install:
ERROR: Could not find a version that satisfies the requirement kedro (from versions: none)
ERROR: No matching distribution found for kedro
Error installing kedro.
This is weird, why cant I run kedro new with pipx? Lets try pip.
pip install kedro
Same issue.
ERROR: Could not find a version that satisfies the requirement kedro (from versions: none)
ERROR: No matching distribution found for kedro
Curious what kedro is? Check out this article.
What’s up #
wrong python version
The issue is that kedro only runs on up to python 3.8, and on Ubuntu
21.10 when you apt install python3 you get python 3.9 and the
standard repos don’t have an old enough version to run kedro.
How to fix this? #
Theres a couple of ways you can fix this? They all involve installing a distribution that does not come from the standard repo.
Where Can I get the right version #
- Anaconda
- Python.org
- deadsnakes
- pyenv
- miniconda
I have two articles that can help you #
How to Install miniconda on linux (from the command line only)
Using miniconda
conda create -n myenv python=3.8
My first impressions with pyenv
Using pyenv
pyenv install 3.8.12