You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

html.py 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """
  2. Copyright (C) 2019 NVIDIA Corporation. All rights reserved.
  3. Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode).
  4. """
  5. import datetime
  6. import dominate
  7. from dominate.tags import *
  8. import os
  9. class HTML:
  10. def __init__(self, web_dir, title, refresh=0):
  11. if web_dir.endswith('.html'):
  12. web_dir, html_name = os.path.split(web_dir)
  13. else:
  14. web_dir, html_name = web_dir, 'index.html'
  15. self.title = title
  16. self.web_dir = web_dir
  17. self.html_name = html_name
  18. self.img_dir = os.path.join(self.web_dir, 'images')
  19. if len(self.web_dir) > 0 and not os.path.exists(self.web_dir):
  20. os.makedirs(self.web_dir)
  21. if len(self.web_dir) > 0 and not os.path.exists(self.img_dir):
  22. os.makedirs(self.img_dir)
  23. self.doc = dominate.document(title=title)
  24. with self.doc:
  25. h1(datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y"))
  26. if refresh > 0:
  27. with self.doc.head:
  28. meta(http_equiv="refresh", content=str(refresh))
  29. def get_image_dir(self):
  30. return self.img_dir
  31. def add_header(self, str):
  32. with self.doc:
  33. h3(str)
  34. def add_table(self, border=1):
  35. self.t = table(border=border, style="table-layout: fixed;")
  36. self.doc.add(self.t)
  37. def add_images(self, ims, txts, links, width=512):
  38. self.add_table()
  39. with self.t:
  40. with tr():
  41. for im, txt, link in zip(ims, txts, links):
  42. with td(style="word-wrap: break-word;", halign="center", valign="top"):
  43. with p():
  44. with a(href=os.path.join('images', link)):
  45. img(style="width:%dpx" % (width), src=os.path.join('images', im))
  46. br()
  47. p(txt.encode('utf-8'))
  48. def save(self):
  49. html_file = os.path.join(self.web_dir, self.html_name)
  50. f = open(html_file, 'wt')
  51. f.write(self.doc.render())
  52. f.close()
  53. if __name__ == '__main__':
  54. html = HTML('web/', 'test_html')
  55. html.add_header('hello world')
  56. ims = []
  57. txts = []
  58. links = []
  59. for n in range(4):
  60. ims.append('image_%d.jpg' % n)
  61. txts.append('text_%d' % n)
  62. links.append('image_%d.jpg' % n)
  63. html.add_images(ims, txts, links)
  64. html.save()

第三届计图人工智能挑战赛——风格及语义引导的风景图片生成赛道项目,由jittor计图框架实现