私の Sphinx ベースのドキュメントには、カスタムの Jinja2 テンプレートを含む特別なページがあります。このテンプレートのコンテキストとして、少しの Python コードで生成されたカスタム オブジェクトを渡したいと思います。(彼らはdatetime
、一種のカレンダーです。) には、次のconf.py
ようなものが含まれます。
import datetime
def add_index(app):
yield 'index', {'today': datetime.datetime.today()}, 'index.html'
def setup(app):
app.connect('html-collect-pages', add_index)
これはローカルでは問題なくビルドされますが、Read the Docs は JSON を使用してシリアル化しようとします。「非 JSON 対応」オブジェクトをテンプレートに渡す方法はありますか?
私が得る例外は次のとおりです。
Running Sphinx v1.3.5
making output directory...
loading translations [cs]... done
loading pickled environment... not yet created
loading intersphinx inventory from http://docs.python.org/objects.inv...
building [mo]: targets for 0 po files that are out of date
building [json]: targets for 1 source files that are out of date
updating environment: 1 added, 0 changed, 0 removed
reading sources... [100%] course/index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] course/index
generating indices... genindex
Traceback (most recent call last):
File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/cmdline.py", line 244, in main
app.build(opts.force_all, filenames)
File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/application.py", line 266, in build
self.builder.build_update()
File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/builders/__init__.py", line 251, in build_update
'out of date' % len(to_build))
File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/builders/__init__.py", line 325, in build
self.finish()
File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/builders/html.py", line 456, in finish
self.finish_tasks.add_task(self.gen_additional_pages)
File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/util/parallel.py", line 40, in add_task
res = task_func()
File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/builders/html.py", line 482, in gen_additional_pages
self.handle_page(pagename, context, template)
File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/builders/html.py", line 1088, in handle_page
self.dump_context(ctx, outfilename)
File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/builders/html.py", line 1070, in dump_context
self.implementation.dump(context, f, *self.additional_dump_args)
File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/util/jsonimpl.py", line 28, in dump
return json.dump(obj, fp, *args, **kwds)
File "/usr/lib/python3.4/json/__init__.py", line 178, in dump
for chunk in iterable:
File "/usr/lib/python3.4/json/encoder.py", line 422, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/usr/lib/python3.4/json/encoder.py", line 396, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.4/json/encoder.py", line 429, in _iterencode
o = _default(o)
File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/util/jsonimpl.py", line 23, in default
return json.JSONEncoder.default(self, obj)
File "/usr/lib/python3.4/json/encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2016, 3, 22, 14, 37, 54, 816952) is not JSON serializable
Exception occurred:
File "/usr/lib/python3.4/json/encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2016, 3, 22, 14, 37, 54, 816952) is not JSON serializable
The full traceback has been saved in /tmp/sphinx-err-wt0rbhbk.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
A bug report can be filed in the tracker at <https://github.com/sphinx-doc/sphinx/issues>. Thanks!