新浪博客

Python3:字典(或者Json)数据转成思维导图xmind文件

2018-10-27 09:05阅读:
一,环境搭建
  1. xmind模块

  1. Flask模块

不赘述
二,工程
该工程通过Flask模块接受来自requests方法post过来的json数据和其他参数,然后在参数指定目录下创建xmind文件。
源码1:
import xmind
# from xmind.core.const import TOPIC_DETACHED
# from xmind.core.markerref import MarkerId


def
dict_ite(dicts, topic):
for key in dicts:
# Create a topic with key
subtopic = topic.addSubTopic()
subtopic.setTitle(key)
if isinstance(dicts[key], dict): # if the key is a dict, then
dict_ite(dicts[key], subtopic)
return True


def dict2xmind(dicts, filename, path):
file_str = path + '/' +filename+'.xmind'
workbook = xmind.load(file_str) # load an existing file or create a new workbook if nothing is found

sheet = workbook.getPrimarySheet() # get the first sheet
sheet.setTitle(filename) # set its title
rtopic_sheet = sheet.getRootTopic() # get the root topic of this sheet
rtopic_sheet.setTitle(filename) # set its title

if dict_ite(dicts, rtopic_sheet):
xmind.save(workbook, file_str)
return True
else:
return False

if __name__ == '__main__':
Temp ={'H1':1,'H2':2,'H3':{'H21':1,'H22':22,'H23':{'H31':1}}}
dict2xmind(Temp, 'Kitty1', path='')
源码2:
# from flask import Flask, g
from flask import Flask, jsonify, request, flash, logging, session, redirect, url_for
from dump import dict2xmind
__all__ = ['app']
app = Flask(__name__)
@app.route('/')
def index():
return '

Welcome to XMind in Python3

'


@app.route('/get', methods=['POST'])
def get_xmind_file():
if request.method == 'POST':
res = request.get_json()
status = False
if 'data_json' and'path' and 'name' in res.keys():
status = dict2xmind(dicts=res['data_json'], filename=res['name'], path=res['path'])
else:
return jsonify(code=200, status=status, message='ok', data='Missing Infos.')
return jsonify(code=200, status=status, message='ok', data='Successfully Generating File.')
if __name__ == '__main__':
# API地址和端口
API_HOST = '0.0.0.0' # http://127.0.0.1
API_PORT = 5005
app.run(host=API_HOST, port=API_PORT)
'''Example in Clients:
Temp ={'H1':1,'H2':2,'H3':{'H21':1,'H22':22,'H23':{'H31':1}}}
res = {'data_json': Temp,'path': 'E:\XMindAPI','name': 'DR'}
result = requests.post('http://localhost:5005/get', json=res) # 或者http://127.0.0.1:5005
print(result.text)
'''


我的更多文章

下载客户端阅读体验更佳

APP专享