Import/Export world info folders

This commit is contained in:
ebolam
2022-08-29 15:17:00 -04:00
parent 2ed9cb4db2
commit f8e9f34631
4 changed files with 102 additions and 18 deletions

View File

@@ -7322,12 +7322,21 @@ def UI_2_export_world_info_folder():
else: else:
data = koboldai_vars.worldinfo_v2.to_json() data = koboldai_vars.worldinfo_v2.to_json()
folder = koboldai_vars.story_name folder = koboldai_vars.story_name
return Response( return Response(
data, json.dumps(data, indent="\t"),
mimetype="application/json", mimetype="application/json",
headers={"Content-disposition": headers={"Content-disposition":
"attachment; filename={}_world_info.json".format(folder)}) "attachment; filename={}_world_info.json".format(folder)}
)
#==================================================================#
# Event triggered when user exports world info folder
#==================================================================#
@socketio.on('upload_world_info_folder')
def UI_2_upload_world_info_folder(data):
json_data = json.loads(data['data'])
koboldai_vars.worldinfo_v2.load_json(json_data, folder=data['folder'])
#==================================================================# #==================================================================#
# Event triggered when user edits phrase biases # Event triggered when user edits phrase biases

View File

@@ -1367,21 +1367,26 @@ class KoboldWorldInfo(object):
"entries": {x: self.world_info[x] for x in self.world_info if self.world_info[x]['folder'] == folder} "entries": {x: self.world_info[x] for x in self.world_info if self.world_info[x]['folder'] == folder}
} }
def load_json(self, data): def load_json(self, data, folder=None):
self.world_info = {int(x): data['entries'][x] for x in data['entries']} if folder is None:
self.world_info_folder = data['folders'] self.world_info = {int(x): data['entries'][x] for x in data['entries']}
#Make sure we have all the appropriate variables: self.world_info_folder = data['folders']
for item in self.world_info: #Make sure we have all the appropriate variables:
for column in ["uid","title","key","keysecondary","folder","constant","content","comment","token_length","selective","used_in_game"]: for item in self.world_info:
if column not in item: for column in ["uid","title","key","keysecondary","folder","constant","content","comment","token_length","selective","used_in_game"]:
item[column] = None if column not in item:
try: item[column] = None
self.sync_world_info_to_old_format() try:
except: self.sync_world_info_to_old_format()
print(self.world_info) except:
print(data) print(self.world_info)
raise print(data)
self.send_to_ui() raise
self.send_to_ui()
else:
for uid, item in data['entries'].items():
self.add_item(item['title'], item['key'], item['keysecondary'], folder, item['constant'], item['manual_text'], item['comment'],
use_wpp=item['use_wpp'], wpp=item['wpp'])
def sync_world_info_to_old_format(self): def sync_world_info_to_old_format(self):
#Since the old UI uses world info entries for folders, we need to make some up #Since the old UI uses world info entries for folders, we need to make some up

View File

@@ -829,6 +829,36 @@ td.server_vars {
text-align: center; text-align: center;
} }
.WI_Folder_Header {
margin-left: 10px;
display: grid;
grid-template-areas: "folder expand title upload_box upload download";
grid-template-columns: 15px 30px auto 0px 25px 25px;
}
.WI_Folder_Header .folder {
grid-area: folder;
}
.WI_Folder_Header .expand {
grid-area: expand;
}
.WI_Folder_Header .title {
grid-area: title;
}
.WI_Folder_Header .upload_box {
grid-area: upload_box;
display: none;
}
.WI_Folder_Header .download {
grid-area: download;
}
#world_info_folder_root.WI_Folder { #world_info_folder_root.WI_Folder {
margin-left: 10px; margin-left: 10px;
} }

View File

@@ -1479,6 +1479,7 @@ function world_info_folder(data) {
title.addEventListener('dragover', dragOver); title.addEventListener('dragover', dragOver);
title.addEventListener('dragleave', dragLeave); title.addEventListener('dragleave', dragLeave);
title.addEventListener('drop', drop); title.addEventListener('drop', drop);
title.classList.add("WI_Folder_Header");
collapse_icon = document.createElement("span"); collapse_icon = document.createElement("span");
collapse_icon.id = "world_info_folder_collapse_"+folder_name; collapse_icon.id = "world_info_folder_collapse_"+folder_name;
collapse_icon.classList.add("wi_folder_collapser"); collapse_icon.classList.add("wi_folder_collapser");
@@ -1490,6 +1491,7 @@ function world_info_folder(data) {
document.getElementById('world_info_folder_expand_'+this.getAttribute("folder")).classList.remove('hidden'); document.getElementById('world_info_folder_expand_'+this.getAttribute("folder")).classList.remove('hidden');
this.classList.add("hidden"); this.classList.add("hidden");
}; };
collapse_icon.classList.add("expand")
title.append(collapse_icon); title.append(collapse_icon);
expand_icon = document.createElement("span"); expand_icon = document.createElement("span");
expand_icon.id = "world_info_folder_expand_"+folder_name; expand_icon.id = "world_info_folder_expand_"+folder_name;
@@ -1502,12 +1504,14 @@ function world_info_folder(data) {
document.getElementById('world_info_folder_collapse_'+this.getAttribute("folder")).classList.remove('hidden'); document.getElementById('world_info_folder_collapse_'+this.getAttribute("folder")).classList.remove('hidden');
this.classList.add("hidden"); this.classList.add("hidden");
}; };
expand_icon.classList.add("expand")
expand_icon.classList.add("hidden"); expand_icon.classList.add("hidden");
title.append(expand_icon); title.append(expand_icon);
icon = document.createElement("span"); icon = document.createElement("span");
icon.classList.add("material-icons-outlined"); icon.classList.add("material-icons-outlined");
icon.setAttribute("folder", folder_name); icon.setAttribute("folder", folder_name);
icon.textContent = "folder"; icon.textContent = "folder";
icon.classList.add("folder");
title.append(icon); title.append(icon);
title_text = document.createElement("span"); title_text = document.createElement("span");
title_text.classList.add("wi_title"); title_text.classList.add("wi_title");
@@ -1520,16 +1524,52 @@ function world_info_folder(data) {
socket.emit("Rename_World_Info_Folder", {"old_folder": this.getAttribute("original_text"), "new_folder": this.textContent}); socket.emit("Rename_World_Info_Folder", {"old_folder": this.getAttribute("original_text"), "new_folder": this.textContent});
} }
} }
title_text.classList.add("title");
title.append(title_text); title.append(title_text);
//create download button //create download button
download = document.createElement("span"); download = document.createElement("span");
download.classList.add("material-icons-outlined"); download.classList.add("material-icons-outlined");
download.classList.add("cursor");
download.setAttribute("folder", folder_name); download.setAttribute("folder", folder_name);
download.textContent = "file_download"; download.textContent = "file_download";
download.onclick = function () { download.onclick = function () {
document.getElementById('download_iframe').src = 'export_world_info_folder?folder='+this.getAttribute("folder"); document.getElementById('download_iframe').src = 'export_world_info_folder?folder='+this.getAttribute("folder");
}; };
download.classList.add("download");
title.append(download); title.append(download);
//upload element
upload_element = document.createElement("input");
upload_element.id = "wi_upload_element_"+folder_name;
upload_element.type = "file";
upload_element.setAttribute("folder", folder_name);
upload_element.classList.add("upload_box");
upload_element.onchange = function () {
var fileList = this.files;
for (file of fileList) {
reader = new FileReader();
reader.folder = this.getAttribute("folder");
reader.onload = function (event) {
socket.emit("upload_world_info_folder", {'folder': event.target.folder, 'filename': file.name, "data": event.target.result});
};
reader.readAsArrayBuffer(file);
}
};
title.append(upload_element);
//create upload button
upload = document.createElement("span");
upload.classList.add("material-icons-outlined");
upload.classList.add("cursor");
upload.setAttribute("folder", folder_name);
upload.textContent = "file_upload";
upload.onclick = function () {
document.getElementById('wi_upload_element_'+this.getAttribute("folder")).click();
//document.getElementById('download_iframe').src = 'export_world_info_folder?folder='+this.getAttribute("folder");
};
upload.classList.add("upload");
title.append(upload);
folder.append(title); folder.append(title);
//create add button //create add button