output.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import json
  2. import logging
  3. from collections.abc import Mapping
  4. from .framework import Global
  5. from .input_ctrl import send_msg, single_input, input_control
  6. def set_title(title):
  7. send_msg('output_ctl', dict(title=title))
  8. def text_print(text, *, ws=None):
  9. msg = dict(command="output", spec=dict(content=text, type='text'))
  10. (ws or Global.active_ws).write_message(json.dumps(msg))
  11. def json_print(obj):
  12. text = "```\n%s\n```" % json.dumps(obj, indent=4, ensure_ascii=False)
  13. text_print(text)
  14. put_markdown = text_print
  15. def put_table(tdata):
  16. """
  17. | \| | | |
  18. | ---- | ---- | ---- | ---- |
  19. | | | | |
  20. | | | | |
  21. | | | | |
  22. :param tdata:
  23. :return:
  24. """
  25. def quote(data):
  26. return data.replace('|', r'\|')
  27. header = "|%s|" % "|".join(map(quote, tdata[0]))
  28. res = [header]
  29. res.append("|%s|" % "|".join(['----'] * len(tdata[0])))
  30. for tr in tdata[1:]:
  31. t = "|%s|" % "|".join(map(quote, tr))
  32. res.append(t)
  33. text_print('\n'.join(res))
  34. def buttons(buttons, onclick=None):
  35. pass