code: def csv(self, client_id):
          
          logging.info('Exporting CSV file [{}]'.format(client_id))
          query = (
              db.session.query(Query)
              .filter_by(client_id=client_id)
              .one()
          )
  
          rejected_tables = security_manager.rejected_datasources(
              query.sql, query.database, query.schema)
          if rejected_tables:
              flash(
                  security_manager.get_table_access_error_msg('{}'.format(rejected_tables)))
              return redirect('/')
          blob = None
          if results_backend and query.results_key:
              logging.info(
                  'Fetching CSV from results backend '
                  '[{}]'.format(query.results_key))
              blob = results_backend.get(query.results_key)
          if blob:
              logging.info('Decompressing')
              json_payload = utils.zlib_decompress_to_string(blob)
              obj = json.loads(json_payload)
              columns = [c['name'] for c in obj['columns']]
              df = pd.DataFrame.from_records(obj['data'], columns=columns)
              logging.info('Using pandas to convert to CSV')
              csv = df.to_csv(index=False, **config.get('CSV_EXPORT'))
          else:
              logging.info('Running a query to turn into CSV')
              sql = query.select_sql or query.executed_sql
              df = query.database.get_df(sql, query.schema)
              # TODO(bkyryliuk): add compression=gzip for big files.
              csv = df.to_csv(index=False, **config.get('CSV_EXPORT'))
          response = Response(csv, mimetype='text/csv')
          response.headers['Content-Disposition'] = f'attachment; filename={query.name}.csv'
          logging.info('Ready to return response')
          return response
docs: Download the query results as csv.