Skip to content

Add Python Comment Stripper web tool with Pyodide#226

Merged
simonw merged 1 commit intomainfrom
claude/python-comment-stripper-RhFc5
Feb 14, 2026
Merged

Add Python Comment Stripper web tool with Pyodide#226
simonw merged 1 commit intomainfrom
claude/python-comment-stripper-RhFc5

Conversation

@simonw
Copy link
Owner

@simonw simonw commented Feb 14, 2026

Build python-comment-stripper.html - a tool you can paste python code into and it will use Pyodide and the Python standard library ast mechanism to produce a copy of that code in another textarea with all comments removed but otherwise unchanged - with a copy to clipboard button

Run "uvx rodney --help" for details on a tool you can use to test this

Add tests to the page itself as well, using the same pattern that's used elsewhere in json-diff

Summary

This PR introduces a new interactive web-based tool for removing comments from Python source code. The tool uses Pyodide to run Python's tokenize and ast modules directly in the browser, providing instant feedback as users paste their code.

Key Changes

  • New HTML file: python-comment-stripper.html - A complete single-file web application featuring:

    • Dual-panel UI with input (Python source) and output (comments removed) textareas
    • Real-time comment stripping with debounced input handling
    • Copy-to-clipboard functionality for the processed output
    • Responsive design that works on mobile and desktop
    • Error handling and display for invalid Python syntax
  • Python implementation: Embedded strip_comments() function that:

    • Uses Python's tokenize module to accurately identify and remove comment tokens
    • Preserves string literals (including hashes within strings)
    • Maintains docstrings, indentation, and overall code structure
    • Handles edge cases like shebangs, f-strings, and multi-line strings
    • Cleans up trailing whitespace on lines where comments were removed
  • Pyodide integration: Loads Pyodide v0.27.5 from CDN to execute Python code in the browser without a backend server

  • Comprehensive test suite: 16 test cases covering:

    • Simple inline and full-line comments
    • String preservation (regular, f-strings, multi-line)
    • Docstrings and decorators
    • Edge cases (empty input, no trailing newlines, type comments)
    • Test results displayed in a collapsible details section

Notable Implementation Details

  • Uses tokenize.generate_tokens() for accurate comment detection rather than regex
  • Reconstructs source code by tracking token positions and filling gaps, ensuring no content is lost
  • Implements proper error handling with fallback to ast.parse() for better error messages
  • Auto-strips comments as user types (300ms debounce to avoid excessive processing)
  • Graceful loading state with banner feedback during Pyodide initialization

https://claude.ai/code/session_01J5SJG2a32nwpVGAVS4cvhZ

Single-page tool that strips comments from Python code using the
standard library tokenize module via Pyodide. Features auto-strip
on input, copy to clipboard, and 16 inline tests covering edge
cases (strings with hashes, f-strings, docstrings, shebangs, etc).

https://claude.ai/code/session_01J5SJG2a32nwpVGAVS4cvhZ
@claude
Copy link
Contributor

claude bot commented Feb 14, 2026

Claude finished @simonw's task —— View job


PR Review: Python Comment Stripper Tool

Todo List

  • Read repository guidelines (CLAUDE.md)
  • Analyze the python-comment-stripper.html file
  • Review code quality and best practices
  • Check for potential bugs and issues
  • Evaluate performance considerations
  • Assess security concerns
  • Review test coverage and quality
  • Provide comprehensive feedback

Review Summary

This is a well-implemented single-file web application that effectively leverages Pyodide to provide Python comment stripping functionality directly in the browser. The implementation demonstrates good attention to detail and follows solid engineering practices.

✅ Strengths

Code Quality & Architecture

  • Single-file design: Excellent choice for a utility tool - easy to deploy and share
  • Clean separation: Well-organized HTML, CSS, and JavaScript sections
  • Modern web standards: Proper use of CSS Grid, semantic HTML, and ES6+ features
  • Responsive design: Mobile-friendly with appropriate breakpoints (python-comment-stripper.html:59)

Python Implementation

  • Robust tokenization: Uses tokenize.generate_tokens() for accurate comment detection (python-comment-stripper.html:182)
  • Smart reconstruction: Handles edge cases by tracking token positions and filling gaps properly (python-comment-stripper.html:193-206)
  • String preservation: Correctly preserves hashes within strings, f-strings, and docstrings
  • Error handling: Fallback to ast.parse() for better error messages when tokenization fails (python-comment-stripper.html:184-186)

User Experience

  • Real-time processing: 300ms debounced auto-stripping provides immediate feedback (python-comment-stripper.html:295)
  • Loading states: Clear progress indication during Pyodide initialization
  • Copy functionality: One-click copy with visual feedback
  • Error display: User-friendly error messages with Python stack trace filtering (python-comment-stripper.html:270-273)

Test Coverage

  • Comprehensive suite: 16 test cases covering diverse scenarios
  • Edge case coverage: Includes empty input, no trailing newlines, type comments, shebangs
  • Visual feedback: Real-time test results display in collapsible section

⚠️ Areas for Improvement

Performance Considerations

  1. CDN dependency: Loading from cdn.jsdelivr.net could fail or be slow (python-comment-stripper.html:300)

    • Consider adding a fallback CDN or local copy option
    • Add connection timeout handling
  2. Memory usage: Large files could impact browser performance

    • Consider adding file size warnings for inputs >1MB
    • Implement streaming for very large files

Security Concerns

  1. CDN integrity: No subresource integrity (SRI) check for Pyodide loading

    <script src="https://cdn.jsdelivr.net/pyodide/v0.27.5/full/pyodide.mjs" 
            integrity="sha384-..." crossorigin="anonymous"></script>
  2. Content Security Policy: No CSP headers defined

    • Consider adding CSP meta tag for additional security

Minor Code Issues

  1. Magic number: The 300ms debounce timeout could be configurable (python-comment-stripper.html:295)
  2. Browser compatibility: navigator.clipboard requires HTTPS in production (python-comment-stripper.html:283)
  3. Error message truncation: Very long Python errors might overflow the display area

Potential Bugs

  1. Edge case in line handling: The line cleaning logic might not handle mixed line endings properly (python-comment-stripper.html:221-229)
  2. Test isolation: Tests could potentially interfere with each other if run concurrently

🔧 Suggested Enhancements

Robustness

// Add error boundary for Pyodide loading
const PYODIDE_TIMEOUT = 30000; // 30 seconds
const loadPromise = Promise.race([
  loadPyodide({ indexURL }),
  new Promise((_, reject) => 
    setTimeout(() => reject(new Error('Pyodide load timeout')), PYODIDE_TIMEOUT)
  )
]);

User Experience

  • Add keyboard shortcut (Ctrl/Cmd+Enter) for manual stripping
  • Include file drag-and-drop support
  • Add syntax highlighting for better readability
  • Implement download functionality for processed code

Testing

  • Add performance tests for large files
  • Include Unicode and special character test cases
  • Test browser compatibility edge cases

📊 Technical Assessment

Aspect Score Notes
Code Quality 9/10 Clean, readable, well-structured
Security 7/10 Good but missing SRI checks
Performance 8/10 Efficient with room for large file optimization
UX/UI 9/10 Polished, responsive, intuitive
Test Coverage 9/10 Comprehensive edge case testing
Documentation 8/10 Good code comments, clear function docs

🎯 Recommendation

✅ Approve with minor suggestions

This is a high-quality implementation that successfully delivers the requested functionality. The Python comment stripping logic is robust and handles edge cases well. The user interface is polished and the test suite is comprehensive.

The suggested improvements are mostly around robustness and security hardening rather than fixing critical issues. The code is ready for production use as-is, with the noted enhancements being future improvements rather than blockers.


@simonw simonw merged commit f763a6e into main Feb 14, 2026
1 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants