apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
62.82k stars 13.88k forks source link

custom superset api to download/export dataset in xlsx format #23424

Closed santoshmallah closed 8 months ago

santoshmallah commented 1 year ago

I have written custom API in superset_config.py file in the custom security manager of apache superset.

@expose('/download_dataset/<int:dataset_id>/xlsx/')
    def download_dataset_xlsx(self, dataset_id):
        # Code to download dataset in XLSX format
        query = db.session.query(Query).filter_by(datasource_id=dataset_id).first()
        if not query:
            raise NotFound()
        result_set = db.session.query(ResultSet).filter_by(query_id=query.id).first()
        if not result_set:
            raise NotFound()
        df = pd.read_msgpack(result_set.data)
        output = io.BytesIO()
        writer = pd.ExcelWriter(output, engine='xlsxwriter')
        df.to_excel(writer, sheet_name='Sheet1', index=False)
        writer.save()
        output.seek(0)
        response = Response()
        response.data = output.read()
        response.headers['Content-Disposition'] = 'attachment; filename="dataset.xlsx"'
        response.mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        return response

after adding this I am getting this error

Found but failed to import local superset_config
Traceback (most recent call last):
  File "c:\superset\venv\lib\site-packages\superset\config.py", line 1355, in <module>
    import superset_config
  File "C:\Users\Santosh Mallah\AppData\Local\Programs\Python\superset_config.py", line 189, in <module>
    from security import OIDCSecurityManager
  File "C:\Users\Santosh Mallah\AppData\Local\Programs\Python\security.py", line 12, in <module>
    from superset.models.sql_lab import Query, ResultSet
  File "c:\superset\venv\lib\site-packages\superset\models\__init__.py", line 17, in <module>
    from . import core, datasource_access_request, dynamic_plugins, sql_lab, user_attributes
  File "c:\superset\venv\lib\site-packages\superset\models\core.py", line 56, in <module>
    from superset import app, db_engine_specs, is_feature_enabled
  File "c:\superset\venv\lib\site-packages\superset\db_engine_specs\__init__.py", line 43, in <module>
    from superset.db_engine_specs.base import BaseEngineSpec
  File "c:\superset\venv\lib\site-packages\superset\db_engine_specs\base.py", line 64, in <module>
    from superset.models.sql_lab import Query
  File "c:\superset\venv\lib\site-packages\superset\models\sql_lab.py", line 54, in <module>
    class Query(Model, ExtraJSONMixin):
  File "c:\superset\venv\lib\site-packages\superset\models\sql_lab.py", line 112, in Query
    user = relationship(security_manager.user_model, foreign_keys=[user_id])
AttributeError: 'NoneType' object has no attribute 'user_model'
Traceback (most recent call last):
  File "C:\superset\venv\Scripts\superset-script.py", line 33, in <module>
    sys.exit(load_entry_point('apache-superset==2.0.1', 'console_scripts', 'superset')())
  File "C:\superset\venv\Scripts\superset-script.py", line 25, in importlib_load_entry_point
    return next(matches).load()
  File "C:\Users\Santosh Mallah\AppData\Local\Programs\Python\Python38\lib\importlib\metadata.py", line 75, in load
    module = import_module(match.group('module'))
  File "C:\Users\Santosh Mallah\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "c:\superset\venv\lib\site-packages\superset\cli\main.py", line 28, in <module>
    from superset.cli.lib import normalize_token
  File "c:\superset\venv\lib\site-packages\superset\cli\lib.py", line 20, in <module>
    from superset import config
  File "c:\superset\venv\lib\site-packages\superset\config.py", line 1355, in <module>
    import superset_config
  File "C:\Users\Santosh Mallah\AppData\Local\Programs\Python\superset_config.py", line 189, in <module>
    from security import OIDCSecurityManager
  File "C:\Users\Santosh Mallah\AppData\Local\Programs\Python\security.py", line 12, in <module>
    from superset.models.sql_lab import Query, ResultSet
  File "c:\superset\venv\lib\site-packages\superset\models\__init__.py", line 17, in <module>
    from . import core, datasource_access_request, dynamic_plugins, sql_lab, user_attributes
  File "c:\superset\venv\lib\site-packages\superset\models\core.py", line 56, in <module>
    from superset import app, db_engine_specs, is_feature_enabled
  File "c:\superset\venv\lib\site-packages\superset\db_engine_specs\__init__.py", line 43, in <module>
    from superset.db_engine_specs.base import BaseEngineSpec
  File "c:\superset\venv\lib\site-packages\superset\db_engine_specs\base.py", line 64, in <module>
    from superset.models.sql_lab import Query
  File "c:\superset\venv\lib\site-packages\superset\models\sql_lab.py", line 54, in <module>
    class Query(Model, ExtraJSONMixin):
  File "c:\superset\venv\lib\site-packages\superset\models\sql_lab.py", line 112, in Query
    user = relationship(security_manager.user_model, foreign_keys=[user_id])
AttributeError: 'NoneType' object has no attribute 'user_model'

why I am getting this error

Checklist

Make sure to follow these steps before submitting your issue - thank you!

Thanks in advance.

rusackas commented 8 months ago

We're not great at supporting custom code here, but you can get help on #customizing-superset on Superset Slack. Here we mostly track bugs for shared code.

Closing this as stale since it's been silent for so long, and we're trying to steer toward a more actionable Issues backlog. If people are still encountering this in current versions (currently 3.x) please re-open this issue, open a new Issue with updated context, or raise a PR to address the problem. Thanks!