2

I have a Python dictionary returned by the sentry method event_from_exception().

I would like to format it as nice HTML.

But I guess this was already invented before.

Required features:

  • open source
  • implemented in Python

Example how to create the event data structure:

import sys
import json

from sentry_sdk.utils import event_from_exception

try:
    raise ValueError()
except Exception as exc:
    event, info = event_from_exception(sys.exc_info(), with_locals=True)
    print(json.dumps(event, indent=2))

A general purpose pretty printing of json is a first step, but something like this is way better: traceback-in-django-debug-view

Here is an example how to json looks like:

{
  "exception": {
    "values": [
      {
        "stacktrace": {
          "frames": [
            {
              "function": "<module>", 
              "abs_path": "/home/foo/tmp/t.py", 
              "pre_context": [
                "import json", 
                "", 
                "from sentry_sdk.utils import event_from_exception", 
                "", 
                "try:"
              ], 
              "lineno": 7, 
              "vars": {
                "exc": "ValueError()", 
                "__builtins__": "<module '__builtin__' (built-in)>", 
                "event_from_exception": "<function event_from_exception at 0x7f620a1d0578>", 
                "__file__": "'tmp/t.py'", 
                "__package__": "None", 
                "sys": "<module 'sys' (built-in)>", 
                "json": "<module 'json' from '/usr/lib/python2.7/json/__init__.pyc'>", 
                "__name__": "'__main__'", 
                "__doc__": "None"
              }, 
              "context_line": "    raise ValueError()", 
              "post_context": [
                "except Exception as exc:", 
                "    event, info = event_from_exception(sys.exc_info(), with_locals=True)", 
                "    print(json.dumps(event, indent=2))", 
                ""
              ], 
              "module": "__main__", 
              "filename": "t.py"
            }
          ]
        }, 
        "type": "ValueError", 
        "value": "", 
        "module": "exceptions", 
        "mechanism": null
      }
    ]
  }, 
  "level": "error"
}
5
  • Possibly related, but no way a match, an old, well received, question of mine : The grand, unified theory of PHP error handling – Mawg says reinstate Monica Sep 26 '18 at 13:38
  • 1
    @Mawg AFAIK sentry has a client/sdk for PHP too. In my case I would like to avoid the server. All that I am currently missing is a nice HTML which gets created from the sentry data structure. – guettli Sep 26 '18 at 13:42
  • Can you post an example of such a structure? – Mawg says reinstate Monica Sep 28 '18 at 9:20
  • 1
    @Mawg I updated the question. It contains a sample json now. – guettli Sep 28 '18 at 10:19
  • 1
    Thanks. I think that will give you abetter chance of getting a dedicated answer. – Mawg says reinstate Monica Sep 28 '18 at 10:20
3

I don't think that what you want is specific to sentry_sdk.

You just want a generic function to "pretty print" a Python dictionary, to HTML.

It is easy enough to code for yourself (and fun with recursion ;-), but others have already invented that particular wheel.

See, for instance:

  • this python dictionary object to html5 json form generator

  • the blog entry Pretty printing a Python dictionary to HTML

  • and this S.O question which says that you can do it with pandas, and shows how this code:

    import pandas as pd

    a = {'Job1': {'2017-01-10': [44, 33, 11, 75, 22]},
    'Job2': {'2017-01-05': [25, 25, 0, 100, 25],
    '2017-01-10': [50, 50, 0, 100, 25]},
    'Job3': {'2017-01-03': [44, 22, 22, 50, 22],
    '2017-01-04': [66, 36, 30, 54, 22],
    '2017-01-06': [88, 52, 36, 59, 22],
    '2017-01-10': [132, 68, 64, 51, 22],
    '2017-01-02': [22, 9, 13, 40, 22],
    '2017-01-08': [110, 52, 58, 47, 22]},
    'Job4': {'2017-01-10': [25, 25, 0, 100, 25]}}

    df = pd.DataFrame(data=a) df = df.fillna(' ').T df

generates this HTML table:
enter image description here

I wanted to take the JSON from your example and run it through each of these, but you did not post it and I cannot generate, as I don't have sentry_sdk.

As I said, I think you want a generic solution, not something specific to sentry_sdk. If you agree, change the title of your question to something like "Generate HTML Table from Python dictionary", as which point, I am sure that @SteveBarnes and others will make more suggestions, possibly better than these.

7
  • 1
    Yes, pretty printing is a first step. You are right. I updated my question. – guettli Sep 26 '18 at 13:03
  • But didn't change the title :-) Ok, now it seems that you are not so worried about JSON as you are about stack trace (unless the trace is given in JSON; do you have an example?). Again, this is nothing that is specific to that SDK and if you ask a generic question you are much more likely to get an answer. – Mawg says reinstate Monica Sep 26 '18 at 13:36
  • 1
    yes, I did not change the title. What title do you think would fit? – guettli Sep 26 '18 at 13:40
  • 1
    I am looking for a way to display this particular structure. – guettli Sep 26 '18 at 13:41
  • 1
    I am deeply relaxed. I am not in a hurry. I don't search for an easy to find solution. I search for a a solution which makes me smile. – guettli Sep 26 '18 at 14:15
1

Since I could not find a solution, I wrote one myself: https://github.com/guettli/sentry-event-to-html

It is a simple python library which takes the json created by the sentry client and creates HTML.

5
  • Please accept your own answer, to show other who read this that it worlks. Of course,you asked on the wrong site; coding questions belong on stackoverflow.com I ca see from your profile that you knwo that now, so this remarks is just for others – Mawg says reinstate Monica Jul 16 '20 at 6:26
  • No, wait, don't accept your won answer, because your GitHub says "State Unfinished". Why did you even ell us about it? That helps no one – Mawg says reinstate Monica Jul 16 '20 at 6:29
  • 1
    @MawgsaysreinstateMonica you say this helps no one. My unfinished project is a first step. You are allowed to improve it. The first step might help people who are able to solve the second step. – guettli Jul 16 '20 at 15:49
  • Agreed + upvote. Apologies. My initial thought was that this is not an answer, but you are correct, we can fork it. Just out of curiosity - 1) will you work on it more? 2) is it/can it be generic? Nothing to do with JSON/Sentinel, just a generic exception handler? – Mawg says reinstate Monica Jul 18 '20 at 6:47
  • @MawgsaysreinstateMonica I won't have time for this during the next months. Feel free to clone it. Or I could even move over the owner ship to you. – guettli Aug 6 '20 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.