Skip to content

docs: getting-started + codegen notebook --demo #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 29, 2025
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
[![Documentation](https://img.shields.io/badge/docs-docs.codegen.com-purple?style=flat-square)](https://docs.codegen.com)
[![Slack Community](https://img.shields.io/badge/slack-community-4A154B?logo=slack&style=flat-square)](https://community.codegen.com)
[![License](https://img.shields.io/badge/Code%20License-Apache%202.0-gray?&color=gray)](https://github.com/codegen-sh/codegen-sdk/tree/develop?tab=Apache-2.0-1-ov-file)
[![Follow on X](https://img.shields.io/twitter/follow/codegen?style=social)](https://x.com/codegen)
[![Follow on X](https://img.shields.io/twitter/follow/codegen?style=social)](https://x.com/codegen)

</div>

Expand Down
95 changes: 58 additions & 37 deletions docs/introduction/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A quick tour of Codegen in a Jupyter notebook.

## Installation

Install [`codegen` on pypi](https://pypi.org/project/codegen/) via [uv](https://github.com/astral-sh/uv):
Install [codegen](https://pypi.org/project/codegen/) on Pypi via [uv](https://github.com/astral-sh/uv):

```bash
uv tool install codegen
Expand All @@ -34,8 +34,8 @@ The [codgen notebook](/cli/notebook) command creates a virtual environment and o
# Navigate to your repository
cd path/to/git/repository

# Initialize codegen and launch Jupyter
codegen notebook
# Initialize codegen and launch Jupyter with a demo notebook
codegen notebook --demo
```

This will:
Expand All @@ -47,7 +47,7 @@ This will:
2. Launch Jupyter Lab with a pre-configured notebook

<Tip>
The notebook comes pre-configured to load your codebase, so you can start
The `notebook --demo` comes pre-configured to load [FastAPI](https://github.com/fastapi/fastapi)'s codebase , so you can start
exploring right away!
</Tip>

Expand All @@ -58,13 +58,17 @@ Instantiating a [Codebase](/api-reference/core/Codebase) will automatically pars
```python
from codegen import Codebase

# Parse a codebase
codebase = Codebase("./")
# Initialize FastAPI codebase
print('Cloning and parsing FastAPI to /tmp/codegen/fastapi...')
codebase = Codebase.from_repo('fastapi/fastapi')

# To initialize an existing local codebase, use this constructor
# codebase = Codebase("path/to/git/repo")
```

<Note>
This will automatically infer the programming language of the codebase and
parse all files in the codebase.
parse all files in the codebase. Learn more about [parsing codebases here](/building-with-codegen/parsing-codebases)
</Note>

<Tip>
Expand All @@ -81,8 +85,10 @@ Here are some common patterns for code navigation in Codegen:

- Iterate over all [Functions](/api-reference/core/Function) with [Codebase.functions](/api-reference/core/Codebase#functions)
- View class inheritance with [Class.superclasses](/api-reference/core/Class#superclasses)
- View function call-sites with [Function.call_sites](/api-reference/core/Function#call-sites)
- View function usages with [Function.usages](/api-reference/core/Function#usages)
- View inheritance hierarchies with [inheritance APIs](https://docs.codegen.com/building-with-codegen/class-api#working-with-inheritance)
- Identify recursive functions by looking at [FunctionCalls](https://docs.codegen.com/building-with-codegen/function-calls-and-callsites)
- View function call-sites with [Function.call_sites](/api-reference/core/Function#call-sites)

```python
# Print overall stats
Expand All @@ -108,7 +114,7 @@ if recursive:
print(f" - {func.name}")
```

### Analyzing Tests
## Analyzing Tests

Let's specifically drill into large test files, which can be cumbersome to manage.

Expand All @@ -135,38 +141,52 @@ for file, num_tests in file_test_counts.most_common()[:5]:
print(f" 💡 Functions: {len(file.functions)}")
```

### Splitting Up Large Test Files
## Splitting Up Large Test Files

Lets split up the largest test files into separate modules for better organization.

This uses Codegen's [codebase.move_to_file(...)](/building-with-codegen/moving-symbols), which will:
- update all imports
- (optionally) move dependencies
- do so very fast ⚡️

Lets split up the largest test files into separate modules for better organization:
While maintaining correctness.

```python
print("\n📦 Splitting Test Files")
filename = 'tests/test_path.py'
print(f"📦 Splitting Test File: {filename}")
print("=" * 50)

# Process top 5 largest test files
for file, num_tests in file_test_counts.most_common()[:5]:
# Create a new directory based on the file name
base_name = file.path.replace('.py', '')
print(f"\n🔄 Processing: {file.filepath}")
print(f" 📊 {num_tests} test classes to split")

# Move each test class to its own file
for test_class in file.classes:
if test_class.name.startswith('Test'):
# Create descriptive filename from test class name
new_file = f"{base_name}/{test_class.name.lower()}.py"
print(f" 📝 Moving {test_class.name} -> {new_file}")

# Codegen handles all the complexity:
# - Creates directories if needed
# - Updates all imports automatically
# - Maintains test dependencies
# - Preserves decorators and docstrings
test_class.move_to_file(new_file)
# Grab a file
file = codebase.get_file(filename)
base_name = filename.replace('.py', '')

# Group tests by subpath
test_groups = {}
for test_function in file.functions:
if test_function.name.startswith('test_'):
test_subpath = '_'.join(test_function.name.split('_')[:3])
if test_subpath not in test_groups:
test_groups[test_subpath] = []
test_groups[test_subpath].append(test_function)

# Print and process each group
for subpath, tests in test_groups.items():
print(f"\\n{subpath}/")
new_filename = f"{base_name}/{subpath}.py"

# Create file if it doesn't exist
if not codebase.has_file(new_filename):
new_file = codebase.create_file(new_filename)
file = codebase.get_file(new_filename)

# Move each test in the group
for test_function in tests:
print(f" - {test_function.name}")
test_function.move_to_file(new_file, strategy="add_back_edge")

# Commit changes to disk
codebase.commit()

```

<Warning>
Expand Down Expand Up @@ -299,14 +319,15 @@ if base_class:
Understand key concepts like working with files, functions, imports, and the
call graph to effectively manipulate code.
</Card>
<Card title="IDE Setup" icon="window" href="/introduction/ide-usage">
Iterate locally with your favorite IDE, work with a debugger and build sophisticated codemods
</Card>
<Card
title="Integrate with AI Tools"
icon="robot"
icon="microchip"
href="/introduction/work-with-ai"
>
Learn how to use Codegen with Cursor, Devin, Windsurf, and more.
</Card>
<Card title="API Reference" icon="code" href="/api-reference">
Explore the complete API documentation for all Codegen classes and methods.
</Card>

</CardGroup>
8 changes: 5 additions & 3 deletions src/codegen/cli/commands/notebook/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ def create_jupyter_dir() -> Path:


@click.command(name="notebook")
@click.option("--background", is_flag=True, help="Run Jupyter Lab in the background")
@click.option("--demo", is_flag=True, help="Create a demo notebook with FastAPI example code")
@requires_init
def notebook_command(session: CodegenSession):
"""Open a Jupyter notebook with the current codebase loaded."""
def notebook_command(session: CodegenSession, background: bool, demo: bool):
"""Launch Jupyter Lab with a pre-configured notebook for exploring your codebase."""
with create_spinner("Setting up Jupyter environment...") as status:
venv = VenvManager()

status.update("Checking Jupyter installation...")
venv.ensure_jupyter()

jupyter_dir = create_jupyter_dir()
notebook_path = create_notebook(jupyter_dir)
notebook_path = create_notebook(jupyter_dir, demo=demo)

status.update("Running Jupyter Lab...")

Expand Down
Loading