-
Notifications
You must be signed in to change notification settings - Fork 13.4k
feat: add Jinja tester PySide6 simple app #15756
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
+506
−0
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2d18982
feat: add Jinja tester PySide6 simple app
pwilkin 0b766c7
Linter fixes
pwilkin 67c291b
Pylint fixes
pwilkin dcbcb7f
Whitespace
pwilkin 8789a73
Merge branch 'ggml-org:master' into jinja-tester
pwilkin 1851fc9
Add commandline support; add formatter; add extensions
pwilkin d1ae1d6
Remove testing actions
pwilkin 3225d17
Silence flake8 warnings for commandline mode
pwilkin a6fbf5d
Apply suggestions from code review
pwilkin de41400
Fix trailing whitespace/newline logic
pwilkin ca6e899
Update scripts/jinja/jinja-tester.py
pwilkin 4ee48fa
Update scripts/jinja/jinja-tester.py
pwilkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
import sys | ||
import json | ||
from PySide6.QtWidgets import ( | ||
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, | ||
QLabel, QPlainTextEdit, QTextEdit, QPushButton | ||
) | ||
from PySide6.QtGui import QColor, QColorConstants, QTextCursor, QTextFormat | ||
from PySide6.QtCore import Qt, QRect, QSize | ||
from jinja2 import Environment, TemplateSyntaxError | ||
|
||
|
||
# ------------------------ | ||
# Line Number Widget | ||
# ------------------------ | ||
class LineNumberArea(QWidget): | ||
def __init__(self, editor): | ||
super().__init__(editor) | ||
self.code_editor = editor | ||
|
||
def sizeHint(self): | ||
return QSize(self.code_editor.line_number_area_width(), 0) | ||
|
||
def paintEvent(self, event): | ||
self.code_editor.line_number_area_paint_event(event) | ||
|
||
|
||
class CodeEditor(QPlainTextEdit): | ||
def __init__(self): | ||
super().__init__() | ||
self.line_number_area = LineNumberArea(self) | ||
|
||
self.blockCountChanged.connect(self.update_line_number_area_width) | ||
self.updateRequest.connect(self.update_line_number_area) | ||
self.cursorPositionChanged.connect(self.highlight_current_line) | ||
|
||
self.update_line_number_area_width(0) | ||
self.highlight_current_line() | ||
|
||
def line_number_area_width(self): | ||
digits = len(str(self.blockCount())) | ||
space = 3 + self.fontMetrics().horizontalAdvance("9") * digits | ||
return space | ||
|
||
def update_line_number_area_width(self, _): | ||
self.setViewportMargins(self.line_number_area_width(), 0, 0, 0) | ||
|
||
def update_line_number_area(self, rect, dy): | ||
if dy: | ||
self.line_number_area.scroll(0, dy) | ||
else: | ||
self.line_number_area.update(0, rect.y(), self.line_number_area.width(), rect.height()) | ||
|
||
if rect.contains(self.viewport().rect()): | ||
self.update_line_number_area_width(0) | ||
|
||
def resizeEvent(self, event): | ||
super().resizeEvent(event) | ||
cr = self.contentsRect() | ||
self.line_number_area.setGeometry(QRect(cr.left(), cr.top(), | ||
self.line_number_area_width(), cr.height())) | ||
|
||
def line_number_area_paint_event(self, event): | ||
from PySide6.QtGui import QPainter | ||
painter = QPainter(self.line_number_area) | ||
painter.fillRect(event.rect(), QColorConstants.LightGray) | ||
|
||
block = self.firstVisibleBlock() | ||
block_number = block.blockNumber() | ||
top = int(self.blockBoundingGeometry(block).translated(self.contentOffset()).top()) | ||
bottom = top + int(self.blockBoundingRect(block).height()) | ||
|
||
while block.isValid() and top <= event.rect().bottom(): | ||
if block.isVisible() and bottom >= event.rect().top(): | ||
number = str(block_number + 1) | ||
painter.setPen(QColorConstants.Black) | ||
painter.drawText(0, top, self.line_number_area.width() - 2, | ||
self.fontMetrics().height(), | ||
Qt.AlignmentFlag.AlignRight, number) | ||
block = block.next() | ||
top = bottom | ||
bottom = top + int(self.blockBoundingRect(block).height()) | ||
block_number += 1 | ||
|
||
def highlight_current_line(self): | ||
extra_selections = [] | ||
if not self.isReadOnly(): | ||
selection = QTextEdit.ExtraSelection() | ||
line_color = QColorConstants.Yellow.lighter(160) | ||
selection.format.setBackground(line_color) # type: ignore | ||
selection.format.setProperty(QTextFormat.Property.FullWidthSelection, True) # type: ignore | ||
selection.cursor = self.textCursor() # type: ignore | ||
selection.cursor.clearSelection() # type: ignore | ||
extra_selections.append(selection) | ||
self.setExtraSelections(extra_selections) | ||
|
||
def highlight_position(self, lineno: int, col: int, color: QColor): | ||
block = self.document().findBlockByLineNumber(lineno - 1) | ||
if block.isValid(): | ||
cursor = QTextCursor(block) | ||
text = block.text() | ||
start = block.position() + max(0, col - 1) | ||
cursor.setPosition(start) | ||
if col <= len(text): | ||
cursor.movePosition(QTextCursor.MoveOperation.NextCharacter, | ||
QTextCursor.MoveMode.KeepAnchor) | ||
|
||
extra = QTextEdit.ExtraSelection() | ||
extra.format.setBackground(color.lighter(160)) # type: ignore | ||
extra.cursor = cursor # type: ignore | ||
|
||
self.setExtraSelections(self.extraSelections() + [extra]) | ||
|
||
def highlight_line(self, lineno: int, color: QColor): | ||
block = self.document().findBlockByLineNumber(lineno - 1) | ||
if block.isValid(): | ||
cursor = QTextCursor(block) | ||
cursor.select(QTextCursor.SelectionType.LineUnderCursor) | ||
|
||
extra = QTextEdit.ExtraSelection() | ||
extra.format.setBackground(color.lighter(160)) # type: ignore | ||
extra.cursor = cursor # type: ignore | ||
|
||
self.setExtraSelections(self.extraSelections() + [extra]) | ||
|
||
def clear_highlighting(self): | ||
self.highlight_current_line() | ||
|
||
|
||
# ------------------------ | ||
# Main App | ||
# ------------------------ | ||
class JinjaTester(QMainWindow): | ||
def __init__(self): | ||
super().__init__() | ||
self.setWindowTitle("Jinja Template Tester") | ||
self.resize(1200, 800) | ||
|
||
central = QWidget() | ||
main_layout = QVBoxLayout(central) | ||
|
||
# -------- Top input area -------- | ||
input_layout = QHBoxLayout() | ||
|
||
# Template editor with label | ||
template_layout = QVBoxLayout() | ||
template_label = QLabel("Jinja2 Template") | ||
template_layout.addWidget(template_label) | ||
self.template_edit = CodeEditor() | ||
template_layout.addWidget(self.template_edit) | ||
input_layout.addLayout(template_layout) | ||
|
||
# JSON editor with label | ||
json_layout = QVBoxLayout() | ||
json_label = QLabel("Context (JSON)") | ||
json_layout.addWidget(json_label) | ||
self.json_edit = CodeEditor() | ||
pwilkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
json_layout.addWidget(self.json_edit) | ||
input_layout.addLayout(json_layout) | ||
|
||
main_layout.addLayout(input_layout) | ||
|
||
# -------- Rendered output area -------- | ||
output_label = QLabel("Rendered Output") | ||
main_layout.addWidget(output_label) | ||
self.output_edit = QPlainTextEdit() | ||
self.output_edit.setReadOnly(True) | ||
main_layout.addWidget(self.output_edit) | ||
|
||
# -------- Render button and status -------- | ||
btn_layout = QHBoxLayout() | ||
self.render_btn = QPushButton("Render") | ||
self.render_btn.clicked.connect(self.render_template) | ||
btn_layout.addWidget(self.render_btn) | ||
self.status_label = QLabel("Ready") | ||
btn_layout.addWidget(self.status_label) | ||
main_layout.addLayout(btn_layout) | ||
|
||
self.setCentralWidget(central) | ||
|
||
def render_template(self): | ||
self.template_edit.clear_highlighting() | ||
self.output_edit.clear() | ||
|
||
template_str = self.template_edit.toPlainText() | ||
json_str = self.json_edit.toPlainText() | ||
|
||
# Parse JSON context | ||
try: | ||
context = json.loads(json_str) if json_str.strip() else {} | ||
except Exception as e: | ||
self.status_label.setText(f"❌ JSON Error: {e}") | ||
return | ||
|
||
env = Environment() | ||
pwilkin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
try: | ||
template = env.from_string(template_str) | ||
output = template.render(context) | ||
self.output_edit.setPlainText(output) | ||
self.status_label.setText("✅ Render successful") | ||
except TemplateSyntaxError as e: | ||
self.status_label.setText(f"❌ Syntax Error (line {e.lineno}): {e.message}") | ||
if e.lineno: | ||
self.template_edit.highlight_line(e.lineno, QColor("red")) | ||
except Exception as e: | ||
# Catch all runtime errors | ||
# Try to extract template line number | ||
lineno = None | ||
tb = e.__traceback__ | ||
while tb: | ||
frame = tb.tb_frame | ||
if frame.f_code.co_filename == "<template>": | ||
lineno = tb.tb_lineno | ||
break | ||
tb = tb.tb_next | ||
|
||
error_msg = f"Runtime Error: {type(e).__name__}: {e}" | ||
if lineno: | ||
error_msg = f"Runtime Error at line {lineno} in template: {type(e).__name__}: {e}" | ||
self.template_edit.highlight_line(lineno, QColor("orange")) | ||
|
||
self.output_edit.setPlainText(error_msg) | ||
self.status_label.setText(f"❌ {error_msg}") | ||
|
||
|
||
if __name__ == "__main__": | ||
app = QApplication(sys.argv) | ||
window = JinjaTester() | ||
window.show() | ||
sys.exit(app.exec()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
PySide6 | ||
jinja2 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.