Skip to content

Replace jinja with fstrings#5720

Merged
adhami3310 merged 39 commits intomainfrom
replace-jinja-with-fstrings
Aug 21, 2025
Merged

Replace jinja with fstrings#5720
adhami3310 merged 39 commits intomainfrom
replace-jinja-with-fstrings

Conversation

@adhami3310
Copy link
Member

an experiment

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 18, 2025

CodSpeed Performance Report

Merging #5720 will improve performances by ×2

Comparing replace-jinja-with-fstrings (55d4c1c) with main (818f722)

Summary

⚡ 6 improvements
✅ 2 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
test_compile_page[_complicated_page] 115.3 ms 90.9 ms +26.76%
test_compile_page[_stateful_page] 15.4 ms 10.7 ms +44.33%
test_compile_stateful[_complicated_page] 769.5 µs 601.9 µs +27.84%
test_compile_stateful[_stateful_page] 295 µs 146.8 µs ×2
test_get_all_imports[_complicated_page] 26.2 ms 22.8 ms +14.98%
test_get_all_imports[_stateful_page] 3.6 ms 3.1 ms +18.79%

@adhami3310 adhami3310 marked this pull request as ready for review August 20, 2025 07:55
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This experimental PR represents a massive architectural refactoring that replaces the entire Jinja2 templating system with Python f-string based templates throughout the Reflex framework. The change affects over 60 files and fundamentally transforms how Reflex generates JavaScript code, configuration files, and custom components.

Core Architecture Changes:

  • Template System Migration: All .jinja2 template files in reflex/.templates/jinja/ have been removed and their functionality moved to Python functions in reflex/compiler/templates.py that use f-strings
  • New _RenderUtils Class: A comprehensive utility class with static methods (render, render_tag, render_condition_tag, etc.) replaces the previous Jinja2 macros for component rendering
  • Direct Function Calls: Template usage changes from template.render(**kwargs) pattern to direct function calls like template_function(**kwargs)
  • Dependency Removal: The jinja2>=3.1.2,<4.0 dependency is completely removed from pyproject.toml

Component System Changes:

  • Tag Structure Refactoring: The contents field is moved from the base Tag class to the specialized Tagless class, with Tagless now being a proper dataclass
  • Rendering Optimizations: New direct render() methods added to components like Bare and Foreach that bypass intermediate object creation
  • Method Simplifications: Component access patterns simplified (e.g., tag.get_index_var_arg()tag.index_var_name)

Template Output Changes:

  • Formatting Differences: The new f-string system generates more compact output - JSX code is now single-line instead of multi-line, trailing commas are removed, and spacing is tightened
  • Import Handling: New merge_parsed_imports utility function and refactored import processing logic
  • Hook Rendering: The _sort_hooks function now returns tuples instead of dictionaries for better performance

Integration Points:

  • Compiler Integration: All template calls in reflex/compiler/compiler.py updated to use new function-based approach
  • Plugin System: Tailwind plugins (tailwind_v3.py, tailwind_v4.py) updated to use new template functions
  • Custom Components: Complete rewrite of custom component generation with inline f-string templates
  • Test System: Extensive test updates to match new output formatting

The refactoring maintains functional equivalence while moving from an external templating engine to native Python string formatting. This change affects the entire code generation pipeline but preserves the same API surface for most user-facing functionality.

Confidence score: 2/5

  • This PR introduces significant architectural changes with high risk of breaking existing functionality
  • Score reflects the massive scope of changes affecting core compilation and rendering systems
  • Critical missing implementations identified that will cause runtime failures (package_json_template and vite_config_template functions not implemented)

55 files reviewed, 5 comments

Edit Code Review Bot Settings | Greptile

@adhami3310 adhami3310 merged commit d9b242a into main Aug 21, 2025
40 of 41 checks passed
@adhami3310 adhami3310 deleted the replace-jinja-with-fstrings branch August 21, 2025 23:21
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