Skip to main content

A tool for analyzing and explaining Python code using AI

Project description

Python Code Explainer with Llama

A powerful tool that analyzes Python code and generates comprehensive documentation and explanations using the Llama language model. Available both as a Streamlit web application and a command-line interface.

Python Code Explainer License: MIT

โœจ Features

  • ๐Ÿ” Automatic extraction of functions, classes, and methods from Python code
  • ๐Ÿ“ Detailed code analysis including arguments, return values, and docstrings
  • ๐Ÿค– AI-powered non-technical explanations using Llama model
  • ๐Ÿ–ฅ๏ธ Multiple interfaces: Web UI and Command Line
  • ๐Ÿ“ฅ Export analysis as well-formatted Word documents
  • ๐Ÿ”„ GitHub Action for automated documentation generation
  • ๐Ÿงฉ Modular architecture for easy extension

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.8 or higher
  • Ollama server running locally (with Llama model installed)

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/python-code-explainer.git
    cd python-code-explainer
    
  2. Install the package in development mode:

    pip install -e .
    

    Or install the required packages directly:

    pip install -r requirements.txt
    
  3. Set up Ollama:

    # Install Ollama (if not already installed)
    # Visit https://ollama.ai for installation instructions
    
    # Start Ollama server in one terminal
    ollama serve
    
    # In another terminal, pull the Llama model
    ollama pull llama2
    

๐Ÿ“– Usage

Web Interface

  1. Start the Streamlit application:

    streamlit run app.py
    

    Or:

    python -m src.code_explainer.cli web
    
  2. Open your web browser to http://localhost:8501

  3. Upload a Python file and explore the analysis

Command Line Interface

Analyze a Python file:

python -m src.code_explainer.cli analyze path/to/your/file.py

Generate documentation:

python -m src.code_explainer.cli document path/to/your/file.py --output docs/

๐Ÿ› ๏ธ How It Works

  1. Code Analysis: The tool parses Python files to extract:

    • Function and class definitions
    • Method signatures with arguments and return types
    • Docstrings and inline comments
    • Import statements and module-level documentation
  2. AI Integration: The extracted information is processed by the Llama model to generate:

    • Clear, non-technical explanations
    • Usage examples
    • Documentation in multiple formats
  3. Output Generation: Results can be:

    • Viewed in the web interface
    • Exported as Word documents
    • Generated automatically via GitHub Actions

๐Ÿ“ฆ Project Structure

.
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ code_explainer/
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ cli.py          # Command line interface
โ”‚   โ”‚   โ”œโ”€โ”€ code_analyzer.py # Core code analysis logic
โ”‚   โ”‚   โ”œโ”€โ”€ document_generator.py # Document generation
โ”‚   โ”‚   โ””โ”€โ”€ llm_integration.py # Llama model integration
โ”‚   โ”œโ”€โ”€ action.py          # GitHub Action entry point
โ”‚   โ””โ”€โ”€ analyzer.py        # Core analysis functionality
โ”œโ”€โ”€ .github/workflows/     # GitHub Actions workflows
โ”œโ”€โ”€ app.py                 # Streamlit web app
โ”œโ”€โ”€ main.py                # Legacy entry point
โ”œโ”€โ”€ requirements.txt       # Python dependencies
โ””โ”€โ”€ setup.py               # Package configuration

GitHub Actions

This project includes a GitHub Actions workflow (.github/workflows/code_analysis.yml) that:

  1. Runs tests on push and pull requests
  2. Performs code analysis using the Python Code Explainer
  3. Generates documentation in Word format
  4. Automatically commits and pushes generated documentation to the repository

Workflow Triggers

  • Push to main branch: Runs tests, performs analysis, and updates documentation
  • Pull requests: Runs tests and performs analysis (does not update documentation)
  • Manual trigger: Can be manually triggered from the Actions tab

Generated Documentation

When the workflow runs on the main branch, it will:

  1. Generate a Word document (docs/code_analysis_YYYYMMDD_HHMMSS.docx)
  2. Include a summary of all analyzed code elements
  3. Provide detailed explanations of the code structure
  4. Automatically commit and push the generated documentation

Required Permissions

The workflow requires the following permissions:

  • contents: write - To commit and push generated documentation
  • pull-requests: write - To update pull request statuses
  • statuses: write - To update commit statuses

These permissions are automatically provided by the default GITHUB_TOKEN. No additional configuration is needed for public repositories. For private repositories, ensure the workflow has the necessary permissions in your repository settings.

๐Ÿ“„ License

MIT License - see LICENSE for details.

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup

  1. Fork and clone the repository
  2. Set up a virtual environment:
    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    pip install -e .[dev]
    
  3. Make your changes and run tests
  4. Submit a pull request

๐Ÿ“ง Contact

For any questions or feedback, please open an issue on the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

code_explainer-0.1.0.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

code_explainer-0.1.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file code_explainer-0.1.0.tar.gz.

File metadata

  • Download URL: code_explainer-0.1.0.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.1

File hashes

Hashes for code_explainer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 de606109f4bf2b4dbc3982d469a3abff88fc5e100a916b3ec609d93f43f7ce15
MD5 0cf17f0c32004037d743e9ac0a7366b3
BLAKE2b-256 e830e64acf8d003b5aee0a2e4a0c404d1f02a2b593c5eeff2f9da51915c19a0a

See more details on using hashes here.

File details

Details for the file code_explainer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: code_explainer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.1

File hashes

Hashes for code_explainer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 825bfb524c70c65cbed87c056eb457dea3d9d070a28db7ae906dfbc535b00215
MD5 df265d30baae6aafaebaa8b5f4dd8e91
BLAKE2b-256 55d9d5b1c36826003f7f07193198519b91f8a9de1f1e28ceba6d33674ecc2e63

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page