Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/deploy-docs-to-azure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Codeflash Docs Publish to Azure Static Web Apps

on:
push:
branches:
- main
paths:
- 'docs/**'
- '.github/workflows/deploy-docs-to-azure.yaml'
workflow_dispatch:

jobs:
build_and_deploy_job:
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
runs-on: ubuntu-latest
name: Build and Deploy Job
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Build And Deploy
id: builddeploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for GitHub integrations (i.e. PR comments)
action: "upload"
###### Repository/Build Configurations ######
app_location: "docs" # App source code path relative to repository root
output_location: "build" # Built app content directory, relative to app_location - optional
###### End of Repository/Build Configurations ######
41 changes: 41 additions & 0 deletions .github/workflows/unit-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: unit-tests

on:
push:
branches: [main]
pull_request:
workflow_dispatch:

jobs:
unit-tests:
strategy:
fail-fast: false
matrix:
python-version: [3.9.18, 3.10.13, 3.11.6, 3.12.1, 3.13.0]
continue-on-error: true
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Install uv
uses: astral-sh/setup-uv@v5
with:
python-version: ${{ matrix.python-version }}
version: "0.5.30"
- name: install poetry as a tool
run: uv tool install poetry

- name: install dependencies
run: uvx poetry install --with dev

- name: Unit tests
run: uvx poetry run pytest tests/ --cov --cov-report=xml

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v5
if: matrix.python-version == '3.12.1'
with:
token: ${{ secrets.CODECOV_TOKEN }}
21 changes: 21 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Dependencies
/node_modules

# Production
/build

# Generated files
.docusaurus
.cache-loader

# Misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
29 changes: 29 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Website

This website is built using [Docusaurus](https://docusaurus.io/), a modern static website generator.

### Installation

```
$ npm install
```

### Local Development

```
$ npm run start
```

This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server.

### Build

```
$ npm run build
```

This command generates static content into the `build` directory and can be served using any static contents hosting service.

### Deployment

Deployment is done automatically via GitHub Actions when the Pull Request changes are merged to the `main` branch.
3 changes: 3 additions & 0 deletions docs/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
};
60 changes: 60 additions & 0 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
sidebar_position: 5
---

# Manual Configuration

Codeflash is installed and configured on a per-project basis.
`codeflash init` should guide you through the configuration process, but if you need to manually configure Codeflash or set advanced settings, you can do so by editing the `pyproject.toml` file in the root directory of your project.

## Configuration Options
Codeflash config looks like the following
```toml
[tool.codeflash]
module-root = "my_module"
tests-root = "tests"
test-framework = "pytest"
formatter-cmds = ["black $file"]
# optional configuration
ignore-paths = ["my_module/build/"]
pytest-cmd = "pytest"
disable-imports-sorting = false
disable-telemetry = false
```
All file paths are relative to the directory of the `pyproject.toml` file.

Required Options:
- `module-root`: The Python module you want Codeflash to optimize going forward. Only code under this directory will be optimized. It should also have an `__init__.py` file to make the module importable.
- `tests-root`: The directory where your tests are located. Codeflash will use this directory to discover existing tests as well as generate new tests.
- `test-framework`: The test framework you use for your project. Codeflash supports `pytest` and `unittest`.

Optional Configuration:
- `ignore-paths`: A list of paths withing the `module-root` to ignore when optimizing code. Codeflash will not optimize code in these paths. Useful for ignoring build directories or other generated code. You can also leave this empty if not needed.
- `pytest-cmd`: The command to run your tests. Defaults to `pytest`. You can specify extra commandline arguments here for pytest.
- `formatter-cmds`: The command line to run your code formatter or linter. Defaults to `["black $file"]`. In the command line `$file` refers to the current file being optimized. The assumption with using tools here is that they overwrite the same file and returns a zero exit code. You can also specify multiple tools here that run in a chain as a toml array. You can also disable code formatting by setting this to `["disabled"]`.
- `ruff` - A recommended way to run ruff linting and formatting is `["ruff check --exit-zero --fix $file", "ruff format $file"]`. To make `ruff check --fix` return a 0 exit code please add a `--exit-zero` argument.
- `disable-imports-sorting`: By default, codeflash uses isort to organize your imports before creating suggestions. You can disable this by setting this field to `true`. This could be useful if you don't sort your imports or while using linters like ruff that sort imports too.
- `disable-telemetry`: Disable telemetry data collection. Defaults to `false`. Set this to `true` to disable telemetry data collection. Codeflash collects anonymized telemetry data to understand how users are using Codeflash and to improve the product. Telemetry does not collect any code data.

## Example Configuration
Here's an example project with the following structure:
```text
acme-project/
|- foo_module/
| |- __init__.py
| |- foo.py
| |- main.py
|- tests/
| |- __init__.py
| |- test_script.py
|- pyproject.toml
```

Here's a sample `pyproject.toml` file for the above project:
```toml
[tool.codeflash]
module-root = "foo_module"
tests-root = "tests"
test-framework = "pytest" # or "unittest"
ignore-paths = []
```
5 changes: 5 additions & 0 deletions docs/docs/getting-started/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"label": "Getting Started",
"position": 2,
"collapsed": false
}
125 changes: 125 additions & 0 deletions docs/docs/getting-started/codeflash-github-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
sidebar_position: 2
---

# Automate Optimization of Pull Requests
<!--- TODO: Add more pictures to guide better --->

Codeflash can automatically optimize your code when new pull requests are opened.

To be able to scan new code for performance optimizations, Codeflash requires a GitHub action workflow to
be installed which runs the Codeflash optimization logic on every new pull request.
If the action workflow finds an optimization, it communicates with the Codeflash GitHub
App through our secure servers and asks it to suggest new changes to the pull request.

This is the most useful way of using Codeflash, where you set it up once and all your new code gets optimized.
So setting this up is highly recommended.

## Prerequisites
- You have a Codeflash API key. If you don't have one, you can generate one from the [Codeflash Webapp](https://app.codeflash.ai/). Make sure you generate the API key with the right GitHub account that has access to the repository you want to optimize.
- You have completed [local installation](/getting-started/local-installation) steps and have a Python project with a `pyproject.toml` file that is configured with Codeflash. If you haven't configured Codeflash for your project yet, you can do so by running `codeflash init` in the root directory of your project.

## Add the Codeflash GitHub Actions workflow

### Guided setup

To add the Codeflash GitHub Actions workflow to your repository, you can run the following command in your project directory:

```bash
codeflash init-actions
```

This will walk you through the process of adding the Codeflash GitHub Actions workflow to your repository.

### All Set up!

Open a new PR to your GitHub project, and you will now see a new actions workflow for Codeflash run. If it finds an optimization,
codeflash-ai bot will comment on your repo with the optimization suggestions.

### Manual Installation (optional)
If you prefer to install the GitHub actions manually, follow the steps below -

#### Add the workflow file
Create a new file in your repository at `.github/workflows/codeflash-optimize.yaml` with the following contents:


```yaml title=".github/workflows/codeflash-optimize.yaml"
name: Codeflash

on:
pull_request:
workflow_dispatch:

jobs:
optimize:
name: Optimize new code in this PR
if: ${{ github.actor != 'codeflash-ai[bot]' }}
runs-on: ubuntu-latest
env:
CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
CODEFLASH_PR_NUMBER: ${{ github.event.number }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
# TODO: Replace the following with your project's Python installation method
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
# TODO: Replace the following with your project's dependency installation method
- name: Install Project Dependencies
run: |
python -m pip install --upgrade pip
# TODO: Replace the following with your project setup method
pip install -r requirements.txt
pip install codeflash
- name: Run Codeflash to optimize code
id: optimize_code
run: |
codeflash
```
You would need to fill in the `#TODO`s in the file above to make it work. Please commit this file to your repository.
If you use a particular Python package manager like Poetry or uv, some helpful configurations are provided below.

#### Config with different Python package managers

The yaml config above is a basic template. Here is how you can run Codeflash with the different Python package managers:

1. Poetry

```yaml
- name: Install Project Dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
poetry install --with dev
- name: Run Codeflash to optimize code
id: optimize_code
run: |
poetry env use python
poetry run codeflash
```
This assumes that you install poetry with pip and have Codeflash dependency in the `dev` section of your `pyproject.toml` file.

2. uv

```yaml
- uses: astral-sh/setup-uv@v4
with:
enable-cache: true
- run: uv sync --group=dev
- name: Run Codeflash to optimize code
run: uv run codeflash
```

#### Add your API key to your repository secrets

Go to your GitHub repository, click **Settings**, and click on **Secrets and
Variables** -> **Actions** on the left sidebar.

Add the following secret:

- `CODEFLASH_API_KEY`: The API key you got from https://app.codeflash.ai/app/apikeys

66 changes: 66 additions & 0 deletions docs/docs/getting-started/local-installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
sidebar_position: 1
---

# Local Installation

Codeflash is installed and configured on a per-project basis.

You can install Codeflash locally for a project by running the following command in the project's virtual environment:

```bash
pip install codeflash
```

:::tip[Codeflash is a Development Dependency]
We recommend installing Codeflash as a development dependency.
It doesn't need to be installed as part of your package requirements.
Codeflash is intended to be used locally and as part of development workflows such as CI.
:::

## Generate a Codeflash API Key

Since Codeflash uses advanced Large Language Models (LLMs) that are hosted in the cloud, you will need to generate an API key to use Codeflash.

To generate an API key, visit the [Codeflash Web App](https://app.codeflash.ai/) and sign up for an account with GitHub login.
<!--- TODO: Do we ask for access to specific repositories here? --->
Once you have signed up, you will be able to generate an API key from the [API Key](https://app.codeflash.ai/app/apikeys) page.
You will need the API key in the next step.

## Automatic Configuration

To configure Codeflash for a project, in the root directory of your project where your `pyproject.toml` file is located, run the following command :

```bash
codeflash init
```

If you don't have a pyproject.toml file yet, the codeflash init command will ask you to create one

:::tip[What's pyproject.toml?]
`pyproject.toml` is a configuration file that is used to specify build tool settings for Python projects.
pyproject.toml is the modern replacement for setup.py and requirements.txt files.
It's the new standard for Python package metadata.
:::

When running `codeflash init`, you will see the following prompts:

```text
1. Enter your Codeflash API key:
2. Which Python module do you want me to optimize going forward? (e.g. my_module)
3. Where are your tests located? (e.g. tests/)
4. Which test framework do you use? (pytest/unittest)
```

After you have answered these questions, Codeflash will be configured for your project.
The configuration will be saved in the `pyproject.toml` file in the root directory of your project.
To understand the configuration options, and set more advanced options, see the [Configuration](/configuration) page.

## Install the Codeflash GitHub App

Finally, if you have not done so already, Codeflash will ask you to install the Github App in your repository. The Codeflash GitHub App allows access to your repository to the codeflash-ai bot to open PRs, review code, and provide optimization suggestions.

Please [install the Codeflash GitHub
app](https://github.com/apps/codeflash-ai/installations/select_target) by choosing the repository you want to install
Codeflash on.
##
Loading
Loading