def download_disco():
disco_uri = request.args.get('uri')
turtle_str = get_turtle_uri(disco_uri, True)
filepath = os.path.join(current_app.root_path, conf.TMP_DIR)
file_name = disco_uri.replace('/', '_') + 'DOWNLOAD.ttl'
with open(filepath + file_name, 'w') as rdf_file:
rdf_file.write(turtle_str)
return send_from_directory(directory=filepath, filename=file_name)
python类root_path()的实例源码
def export_table():
ILLEGAL_CHARACTERS_RE = re.compile('[\000-\010]|[\013-\014]|[\016-\037]')
table_name = request.args.get('table_name')
if os.path.isfile(current_app.root_path + "/xlsx/%s.xlsx" % table_name):
pass
else:
conn = g.db.conn
cur = conn.cursor()
sql = "select * from %s" % table_name
cur.execute(sql)
data = cur.fetchall()
wbk = Workbook()
sheet = wbk.create_sheet(table_name, 0)
sheet.cell(row=1, column=1).value = 'uid'
sheet.cell(row=1, column=2).value = 'name'
sheet.cell(row=1, column=3).value = 'address'
sheet.cell(row=1, column=4).value = 'tag'
sheet.cell(row=1, column=5).value = 'small tag'
sheet.cell(row=1, column=6).value = 'location'
sheet.cell(row=1, column=7).value = 'tel'
sheet.cell(row=1, column=8).value = 'pro_name'
sheet.cell(row=1, column=9).value = 'pro_center'
sheet.cell(row=1, column=10).value = 'city_name'
sheet.cell(row=1, column=11).value = 'city_center'
sheet.cell(row=1, column=12).value = 'district_name'
sheet.cell(row=1, column=13).value = 'district_center'
sheet.cell(row=1, column=14).value = 'photo_exists'
sheet.cell(row=1, column=15).value = 'photo1'
sheet.cell(row=1, column=16).value = 'photo2'
sheet.cell(row=1, column=17).value = 'photo3'
i = 2
for each in data:
for m in range(1, 18):
tmp = ILLEGAL_CHARACTERS_RE.sub('', each[m])
sheet.cell(row=i, column=m).value = tmp
i += 1
wbk.save(current_app.root_path + "/xlsx/%s.xlsx" % table_name)
response = make_response(send_from_directory(current_app.root_path + "/xlsx", table_name+".xlsx"))
response.headers["Content-Disposition"] = "attachment; filename=%s.xlsx;" % table_name
return response
# if os.path.isfile(os.path.join('./xlsx', table_name+".xlsx")):
# return send_from_directory('./xlsx', table_name+".xlsx", as_attachment=True)
# else:
# return "fail"
def export_table():
ILLEGAL_CHARACTERS_RE = re.compile('[\000-\010]|[\013-\014]|[\016-\037]')
table_name = request.args.get('table_name')
if os.path.isfile(current_app.root_path + "/xlsx/%s.xlsx" % table_name):
pass
else:
conn = g.db.conn
cur = conn.cursor()
sql = "select * from %s" % table_name
cur.execute(sql)
data = cur.fetchall()
wbk = Workbook()
sheet = wbk.create_sheet(table_name, 0)
sheet.cell(row=1, column=1).value = 'uid'
sheet.cell(row=1, column=2).value = 'name'
sheet.cell(row=1, column=3).value = 'branch_name'
sheet.cell(row=1, column=4).value = 'stars'
sheet.cell(row=1, column=5).value = 'address'
sheet.cell(row=1, column=6).value = 'center'
sheet.cell(row=1, column=7).value = 'price'
sheet.cell(row=1, column=8).value = 'tel'
sheet.cell(row=1, column=9).value = 'comments'
sheet.cell(row=1, column=10).value = 'category_name'
sheet.cell(row=1, column=11).value = 'sub_category_name'
sheet.cell(row=1, column=12).value = 'district'
sheet.cell(row=1, column=13).value = 'region_name'
sheet.cell(row=1, column=14).value = 'taste'
sheet.cell(row=1, column=15).value = 'environment'
sheet.cell(row=1, column=16).value = 'services'
sheet.cell(row=1, column=17).value = 'has_deals'
sheet.cell(row=1, column=18).value = 'bookable'
sheet.cell(row=1, column=19).value = 'has_takeaway'
sheet.cell(row=1, column=20).value = 'has_mobilepay'
sheet.cell(row=1, column=21).value = 'has_promote'
sheet.cell(row=1, column=22).value = 'opentime'
sheet.cell(row=1, column=23).value = 'city'
sheet.cell(row=1, column=24).value = 'pro'
sheet.cell(row=1, column=25).value = 'dish1'
sheet.cell(row=1, column=26).value = 'dish2'
sheet.cell(row=1, column=27).value = 'dish3'
sheet.cell(row=1, column=28).value = 'dish4'
sheet.cell(row=1, column=29).value = 'dish5'
i = 2
for each in data:
for m in range(1, 23):
tmp = ILLEGAL_CHARACTERS_RE.sub('', each[m])
sheet.cell(row=i, column=m).value = tmp
sheet.cell(row=i, column=23).value = each[24]
sheet.cell(row=i, column=24).value = each[25]
dishes = each[23]
dish_list = dishes.split(' ')
dish_len = len(dish_list) if len(dish_list) <= 5 else 5
for x in range(0, dish_len):
sheet.cell(row=i, column=25+x).value = dish_list[x]
i += 1
wbk.save(current_app.root_path + "/xlsx/%s.xlsx" % table_name)
response = make_response(send_from_directory(current_app.root_path + "/xlsx", table_name+".xlsx"))
response.headers["Content-Disposition"] = "attachment; filename=%s.xlsx;" % table_name
return response