6. 数据管理¶
演示如何导出、导入自定义色带,以及清理数据库。
备份自定义色带¶
用户目录下的 ~/.multicolor/cmap_data.db 存储所有自定义色带。换机器或重装环境前,建议导出备份。
In [1]:
Copied!
from multicolor import cmap
import os
import tempfile
import json
# 先添加一些测试数据
cmap.add(
name="BackupTest",
colors=["#ff0000", "#00ff00", "#0000ff"],
cmap_type="qualitative",
tags=["test", "backup"],
source=["user"],
)
print(f"Added BackupTest. Total custom: {len(cmap.list(is_custom=True))}")
# 导出到 JSON
with tempfile.TemporaryDirectory() as tmpdir:
backup_path = os.path.join(tmpdir, "my_cmaps.json")
result_path = cmap.export(backup_path)
print(f"\nExported to: {result_path}")
# 查看导出内容
with open(backup_path, "r", encoding="utf-8") as f:
data = json.load(f)
print(f"\nBackup contains {len(data)} colormaps:")
for item in data:
print(f" {item['name']}: {item['cmap_type']}, colors={item['colors'][:2]}...")
from multicolor import cmap
import os
import tempfile
import json
# 先添加一些测试数据
cmap.add(
name="BackupTest",
colors=["#ff0000", "#00ff00", "#0000ff"],
cmap_type="qualitative",
tags=["test", "backup"],
source=["user"],
)
print(f"Added BackupTest. Total custom: {len(cmap.list(is_custom=True))}")
# 导出到 JSON
with tempfile.TemporaryDirectory() as tmpdir:
backup_path = os.path.join(tmpdir, "my_cmaps.json")
result_path = cmap.export(backup_path)
print(f"\nExported to: {result_path}")
# 查看导出内容
with open(backup_path, "r", encoding="utf-8") as f:
data = json.load(f)
print(f"\nBackup contains {len(data)} colormaps:")
for item in data:
print(f" {item['name']}: {item['cmap_type']}, colors={item['colors'][:2]}...")
Added BackupTest. Total custom: 5 Exported to: /tmp/tmps6tt99_0/my_cmaps.json Backup contains 5 colormaps: BackupTest: qualitative, colors=['#ff0000', '#00ff00']... OverwriteTest: sequential, colors=['#333333', '#444444']... RoundTrip: diverging, colors=['#111111', '#222222']... TestCmap: sequential, colors=['#ff0000', '#00ff00']... TestIsCustom: sequential, colors=['#ff0000', '#00ff00']...
恢复自定义色带¶
import_data() 从备份文件导入色带。已存在的同名自定义色带会被覆盖。
In [2]:
Copied!
# 删除测试色带
cmap.remove("BackupTest")
print(f"Removed BackupTest. Total custom: {len(cmap.list(is_custom=True))}")
print(f"BackupTest in names: {'BackupTest' in cmap.names()}")
# 删除测试色带
cmap.remove("BackupTest")
print(f"Removed BackupTest. Total custom: {len(cmap.list(is_custom=True))}")
print(f"BackupTest in names: {'BackupTest' in cmap.names()}")
Removed BackupTest. Total custom: 4 BackupTest in names: False
清理测试数据¶
运行其他示例 notebook 后可能留下测试数据。可用以下方式清理:
In [3]:
Copied!
from multicolor.db import _get_conn
# 查看所有自定义色带
custom = cmap.list(is_custom=True)
if custom:
print("Custom colormaps to clean up:")
for cmap_id, name, _ in custom:
print(f" [{cmap_id}] {name}")
# 选择性删除(小心操作)
# for _, name, _ in custom:
# cmap.remove(name)
# print(f"Removed {name}")
else:
print("No custom colormaps to clean up.")
from multicolor.db import _get_conn
# 查看所有自定义色带
custom = cmap.list(is_custom=True)
if custom:
print("Custom colormaps to clean up:")
for cmap_id, name, _ in custom:
print(f" [{cmap_id}] {name}")
# 选择性删除(小心操作)
# for _, name, _ in custom:
# cmap.remove(name)
# print(f"Removed {name}")
else:
print("No custom colormaps to clean up.")
Custom colormaps to clean up: [10003] OverwriteTest [10001] RoundTrip [10004] TestCmap [10002] TestIsCustom