add markdown log option

This commit is contained in:
冯不游 2022-09-16 17:21:54 +08:00 committed by Lincoln Stein
parent e33ed45cfc
commit a28636dd4a

View File

@ -292,8 +292,8 @@ def main_loop(gen, opt, infile):
continue
print('Outputs:')
log_path = os.path.join(current_outdir, 'dream_log.txt')
write_log_message(results, log_path)
log_path = os.path.join(current_outdir, 'dream_log')
write_log_message(results, log_path ,('txt', 'md'))
print()
print('goodbye!')
@ -339,17 +339,29 @@ def dream_server_loop(gen, host, port, outdir):
dream_server.server_close()
def write_log_message(results, log_path):
def write_log_message(results, log_path, file_types):
"""logs the name of the output image, prompt, and prompt args to the terminal and log file"""
global output_cntr
log_lines = [f'{path}: {prompt}\n' for path, prompt in results]
for l in log_lines:
output_cntr += 1
print(f'[{output_cntr}] {l}',end='')
with open(log_path, 'a', encoding='utf-8') as file:
for file_type in file_types:
if file_type == 'txt':
with open(log_path + '.txt', 'a', encoding='utf-8') as file:
file.writelines(log_lines)
elif file_type == 'md' or file_type == 'markdown':
md_lines = []
for path,prompt in results:
file_name = os.path.basename(path)
md_lines.append(f'## {file_name}\n![]({file_name})\n{prompt}\n')
with open(log_path + '.md', 'a', encoding='utf-8') as file:
file.writelines(md_lines)
else:
print(f"'{file_type}' format is not supported, so write in plain text")
with open(log_path + '.' + file_type, 'a', encoding='utf-8') as file:
file.writelines(log_lines)
if __name__ == '__main__':
main()