PyTraceToIX - How to debug Jinja2 templates, Flask web apps without breaking the design or code changes

PyTraceToIX - How to debug Jinja2 templates, Flask web apps without breaking the design or code changes

PyTraceToIX is an expression tracer designed for debugging Jinja2 templates, Flask web apps, lambdas, list comprehensions, method chaining, and expressions in general.

Code editors often cannot set breakpoints within these kinds of expressions, which requires significant code modifications to debug effectively.

For Jinja2 templates, the debug extension can be used, but it typically dumps the entire context, making it difficult to isolate specific issues.

PyTraceToIX solves this by allowing developers to trace and write specific data directly to sys.stdout or a stream without altering the design or making any changes to the web application.

Additionally, PyTraceToIX can capture multiple inputs and their results, displaying them all in a single line, making it easier to view aggregated data and trace the flow of values.

PyTraceToIX offers a straightforward solution to these challenges, simplifying debugging while preserving the integrity of the original codebase.

It was designed to be simple, with easily identifiable functions that can be removed once the bug is found.

PyTraceToIX has 2 major functions:

  • c__ capture the input of an expression input. ex: c__(x)
  • d__ display the result of an expression and all the captured inputs. ex: d__(c__(x) + c__(y))

And 2 optional functions:

  • init__ initializes display format, output stream and multithreading.
  • t__ defines a name for the current thread.

Features

  • No external dependencies.
  • Minimalist function names that are simple and short.
  • Traces Results along with Inputs.
  • Configurable Result and Input naming.
  • Output to the stdout or a stream.
  • Supports multiple levels.
  • Capture Input method with customizable allow and name callbacks.
  • Display Result method with customizable allow, before, and after callbacks.
  • Result and Inputs can be reformatted and overridden.
  • Configurable formatting at global level and at function level.
  • Multithreading support.

Example

  • A flask web app uses a Jinja2 template
  • It generates a shopping card html table with product, quantity and final price.
  • The product name is only the first 11 characters, but we need to know the full name.
  • It only shows the final price which is Price * Qty - discount.
  • The discount is dependent of the quantity.
  • c__ captures the complete name but doesn't change the design.
  • c__ captures the qty and labels it as Qty.
  • c__ captures the discount value.
  • d__ outputs to sys.stdout all the captured inputs and the final price.

From the image, you can see that the Web page design remain intact, while stdout provide a richer aggregated data line by line.


Alexandre Bento Freire

???? Software Engineer ?? DevToIX and TravelToIX

5 个月

?? Please upvote, if you have have Reddit. ?? Your vote counts. Help me to spread out the word. https://reddit.com/r/Python/comments/1g6fd28/pytracetoix_debugging_jinja2_template_flask_web/

要查看或添加评论,请登录

Alexandre Bento Freire的更多文章

社区洞察

其他会员也浏览了