Hannes221 / rq-dashboard-fast

Redis Queue Dashboard based on FastAPI
MIT License
86 stars 8 forks source link

Colored tracebacks #91

Closed jonashaag closed 3 months ago

jonashaag commented 3 months ago

Would be nice to have colored tracebacks on the job page.

Unfortunately we can't use something like rich.traceback because RQ doesn't store the original exception, just a string.

But we can use pygments to add some color. Example proof of concept:

diff --git rq_dashboard_fast/rq_dashboard_fast.py rq_dashboard_fast/rq_dashboard_fast.py
index ff39011..8f91edd 100644
--- rq_dashboard_fast/rq_dashboard_fast.py
+++ rq_dashboard_fast/rq_dashboard_fast.py
@@ -216,14 +216,21 @@ class RedisQueueDashboard(FastAPI):
         @self.get("/job/{job_id}", response_model=JobDataDetailed)
         async def get_job_data(job_id: str, request: Request):
             try:
                 job = get_job(self.redis_url, job_id)
+                if job.exc_info:
+                    from pygments import highlight
+                    from pygments.lexers import PythonTracebackLexer
+                    from pygments.formatters import HtmlFormatter
+                    css = HtmlFormatter().get_style_defs()
+                    col_exc_info = highlight(job.exc_info, PythonTracebackLexer(), HtmlFormatter())
+                else:
+                    col_exc_info = None
+                    css = None

                 active_tab = "job"

@@ -234,6 +241,8 @@ class RedisQueueDashboard(FastAPI):
                     {
                         "request": request,
                         "job_data": job,
+                        "col_exc_info": col_exc_info,
+                        "css": css,
                         "active_tab": active_tab,
                         "prefix": prefix,
                         "rq_dashboard_version": self.rq_dashboard_version,
diff --git rq_dashboard_fast/templates/job.html rq_dashboard_fast/templates/job.html
index b2237c2..bf09e10 100644
--- rq_dashboard_fast/templates/job.html
+++ rq_dashboard_fast/templates/job.html
@@ -6,6 +6,9 @@
   <meta charset="UTF-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <title>Job Details</title>
+  <style>
+      {{ css|safe }}
+  </style>
   <style>
     .back-button-container {
       display: flex;
@@ -61,15 +64,15 @@
       </tr>
       <tr>
         <th>Result</th>
-        <td class="table-cell">{{ job_data.result }}</td>
+        <td class="table-cell"><pre>{{ job_data.result }}</pre></td>
       </tr>
       <tr>
         <th>Exception Info</th>
-        <td class="table-cell">{{ job_data.exc_info }}</td>
+        <td class="table-cell">{% if col_exc_info %}{{ col_exc_info|safe }}{% else %}<pre>{{ job_data.exc_info }}{% endif %}</pre></td>
       </tr>
       <tr>
         <th>Meta</th>
-        <td class="table-cell">{{ job_data.meta }}</td>
+        <td class="table-cell"><pre>{{ job_data.meta }}</pre></td>
       </tr>
     </table>
     <div class="back-button-container">
@@ -79,4 +82,4 @@
 </body>

 </html>
-{% endblock %}
\ No newline at end of file
+{% endblock %}
jonashaag commented 3 months ago
image
Hannes221 commented 3 months ago

Thank you @jonashaag! Will be included in the next release.