mirror of
https://github.com/apache/superset.git
synced 2026-04-19 16:14:52 +00:00
feat: import configuration from directory (#15686)
* feat: command to import configuration from a directory This allows us to keep Superset updated from a repo: ```bash $ superset import-directory /path/to/configs/ ``` For example, I created a simple dashboard with a single chart: PLACEHOLDER I then exported it to a file `dashboard_export_20210714T104600.zip` and unzipped it. After deleting the dashboard, chart, dataset, and database I imported everything back with: ```bash $ superset import-directory ~/Downloads/dashboard_export_20210714T104600/ ``` I then changed the chart title in `~/Downloads/dashboard_export_20210714T104600/charts/Cnt_per_country_1.yaml` and ran the command again. The chart was succesfully updated: PLACEHOLDER * Small fixes
This commit is contained in:
@@ -30,5 +30,5 @@ from .paris import load_paris_iris_geojson
|
||||
from .random_time_series import load_random_time_series_data
|
||||
from .sf_population_polygons import load_sf_population_polygons
|
||||
from .tabbed_dashboard import load_tabbed_dashboard
|
||||
from .utils import load_from_configs
|
||||
from .utils import load_examples_from_configs
|
||||
from .world_bank import load_world_bank_health_n_pop
|
||||
|
||||
@@ -14,18 +14,29 @@
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
import logging
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict
|
||||
|
||||
import yaml
|
||||
from pkg_resources import resource_isdir, resource_listdir, resource_stream
|
||||
|
||||
from superset.commands.exceptions import CommandInvalidError
|
||||
from superset.commands.importers.v1.examples import ImportExamplesCommand
|
||||
from superset.commands.importers.v1.utils import METADATA_FILE_NAME
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
YAML_EXTENSIONS = {".yaml", ".yml"}
|
||||
|
||||
|
||||
def load_from_configs(force_data: bool = False, load_test_data: bool = False) -> None:
|
||||
def load_examples_from_configs(
|
||||
force_data: bool = False, load_test_data: bool = False
|
||||
) -> None:
|
||||
"""
|
||||
Load all the examples inside superset/examples/configs/.
|
||||
"""
|
||||
contents = load_contents(load_test_data)
|
||||
command = ImportExamplesCommand(contents, overwrite=True, force_data=force_data)
|
||||
command.run()
|
||||
@@ -55,3 +66,35 @@ def load_contents(load_test_data: bool = False) -> Dict[str, Any]:
|
||||
)
|
||||
|
||||
return {str(path.relative_to(root)): content for path, content in contents.items()}
|
||||
|
||||
|
||||
def load_configs_from_directory(
|
||||
root: Path, overwrite: bool = True, force_data: bool = False
|
||||
) -> None:
|
||||
"""
|
||||
Load all the examples from a given directory.
|
||||
"""
|
||||
contents: Dict[str, str] = {}
|
||||
queue = [root]
|
||||
while queue:
|
||||
path_name = queue.pop()
|
||||
if path_name.is_dir():
|
||||
queue.extend(path_name.glob("*"))
|
||||
elif path_name.suffix.lower() in YAML_EXTENSIONS:
|
||||
with open(path_name) as fp:
|
||||
contents[str(path_name.relative_to(root))] = fp.read()
|
||||
|
||||
# removing "type" from the metadata allows us to import any exported model
|
||||
# from the unzipped directory directly
|
||||
metadata = yaml.load(contents.get(METADATA_FILE_NAME, "{}"))
|
||||
if "type" in metadata:
|
||||
del metadata["type"]
|
||||
contents[METADATA_FILE_NAME] = yaml.dump(metadata)
|
||||
|
||||
command = ImportExamplesCommand(
|
||||
contents, overwrite=overwrite, force_data=force_data
|
||||
)
|
||||
try:
|
||||
command.run()
|
||||
except CommandInvalidError as ex:
|
||||
_logger.error("An error occurred: %s", ex.normalized_messages())
|
||||
|
||||
Reference in New Issue
Block a user