import os
import random
import time
import pandas as pd
from Simple_Drawing.settings import BASE_DIR
url_ = "127.0.0.1:8000"
def random_color():
color_list = ["#009688", '#5FB878', '#393D49',
'#1E9FFF', '#FFB800', '#FF5722',
'#01AAED', '#2F4056']
return random.choice(color_list)
# 保存单个文件
def keep_file(file_data, file_path, file_name=None, ui=0):
if file_name is not None:
file_path = os.path.join(file_path, file_name)
if ui:
with open(file_path, 'w') as f:
f.write(file_data)
else:
with open(file_path, 'wb+') as f:
for chunk1 in file_data.chunks():
f.write(chunk1)
# 保存多个文件
def keep_files(na, c):
cmd = '''
cd %s/file_keep/%s
mkdir morefile
''' % (BASE_DIR, c)
os.system(cmd)
for file in na:
with open('%s' % os.path.join(f'{BASE_DIR}/file_keep/{c}/morefile/', file.name), 'wb') as f:
for i in file.chunks():
f.write(i)
f.close()
# 返回唯一文件编号
def generate_id():
a = time.asctime(time.localtime(time.time()))
b = a[-15:] + str(random.randint(0, 1000))
c = b.replace(" ", "")
c = c.replace(":", "0")
return c
# 给出文件路径
def path_out(sort, name=generate_id(), show=None, out=None):
if show == 1:
return os.path.join(BASE_DIR, "static", "img_eg", "show_chart", name)
if out == 1:
return os.path.join(BASE_DIR, 'file_keep', sort, "img", name)
return os.path.join(BASE_DIR, 'file_keep', sort, "file", name), name
# 选出需要的列
def pick_col(file_path, col, sep=","):
midwest = pd.read_csv(file_path, sep=sep)
col = set(midwest[col])
return col
# 返回页数
def return_pages(su: int) -> int:
if su % 12 != 0:
return (su // 12) + 1
else:
return su // 12
class ChartParameter(object):
def __init__(self, data):
if 'name' in data:
self.name = data['name']
if 'html_name' in data:
self.html_name = data['html_name']
if 'value' in data:
self.value = data['value']
if 'astrict' in data:
self.astrict = data['astrict']
if 'input_type' in data:
self.input_type = data['input_type']
if 'Basics' in data:
self.Basics = data['Basics']
if 'Senior' in data:
self.Senior = data['Senior']
self.colors_dict = {}
def html_out_Basics(self):
code = ""
for title in self.Basics:
code += f"""
"""
for d in self.Basics[title]:
parameter = ChartParameter(d)
if parameter.input_type == "text":
parameter.input_type = f"""
"""
if parameter.input_type == "number":
parameter.input_type = f"""
"""
if parameter.input_type == "date":
parameter.input_type = f"""
"""
if parameter.input_type == "color_name":
self.colors_dict[f"{parameter.name}"] = "color_name"
parameter.input_type = f"""
"""
if parameter.input_type == "rgb":
self.colors_dict[f"{parameter.name}"] = "rgb"
f"""