Browse Source

[slack] Use tabulate module instead of Prettytable to make result table

Advantages:
- Compatible with Py2 so no ImportError check
- Faster than Prettytable( almost 2x )
Harshg999 4 years ago
parent
commit
88bdff1ae4
2 changed files with 4 additions and 10 deletions
  1. 1 1
      desktop/core/requirements.txt
  2. 3 9
      desktop/core/src/desktop/lib/botserver/views.py

+ 1 - 1
desktop/core/requirements.txt

@@ -40,7 +40,6 @@ mysqlclient==1.4.6
 nose==1.3.7
 openpyxl==2.6.2
 phoenixdb==1.0.0
-prettytable==2.1.0
 pyformance==0.3.2
 pylint==2.6.0
 pylint-django==2.3.0
@@ -62,6 +61,7 @@ slack-sdk==3.2.0
 SQLAlchemy==1.3.8
 sqlparse==0.4.1
 tablib==0.13.0
+tabulate==0.8.9
 thrift==0.13.0
 thrift-sasl==0.4.2
 git+https://github.com/gethue/django-babel.git

+ 3 - 9
desktop/core/src/desktop/lib/botserver/views.py

@@ -19,6 +19,7 @@ import logging
 import json
 from urllib.parse import urlsplit
 from pprint import pprint
+from tabulate import tabulate
 
 from desktop import conf
 from desktop.conf import ENABLE_GIST_PREVIEW
@@ -38,11 +39,6 @@ from django.views.decorators.csrf import csrf_exempt
 
 LOG = logging.getLogger(__name__)
 
-try:
-  from prettytable import PrettyTable
-except ImportError:
-  LOG.warn('slack server: prettytable module is not installed')
-
 SLACK_VERIFICATION_TOKEN = conf.SLACK.SLACK_VERIFICATION_TOKEN.get()
 SLACK_BOT_USER_TOKEN = conf.SLACK.SLACK_BOT_USER_TOKEN.get()
 
@@ -139,15 +135,13 @@ def query_result(request, notebook):
 
   return 'Query result has expired or could not be found'
 
+
 def _make_result_table(result):
   meta = []
   for field in result['meta']:
     meta.append(field['name'])
 
-  table = PrettyTable()
-  table.field_names = meta
-  table.add_rows(result['data'])
-  return table
+  return tabulate(result['data'], headers=meta, tablefmt="plain")
 
 
 def _make_unfurl_payload(url, id_type, doc, doc_type):