Execute Python code after the documentation has been built

54 views
Skip to first unread message

Zoltan

unread,
Oct 22, 2020, 6:34:33 AM10/22/20
to sphinx-users

I need to copy a file before the documentation generation starts. I put this to the beginning of conf.py:

from shutil import copy
copy('../../README.md', '.')

I want to delete this file after the documentation has been generated. How can I instruct Sphinx to execute code after the generation?

Komiya Takeshi

unread,
Oct 25, 2020, 12:15:32 PM10/25/20
to sphinx...@googlegroups.com
Hi,

How about add an instruction into Makefile? I believe it's very simple!

Thanks,
Takeshi KOMIYA

2020年10月22日(木) 19:34 Zoltan <kova...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "sphinx-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/bb5f66de-4664-4673-a293-37e3eacd314cn%40googlegroups.com.

Eric Larson

unread,
Oct 25, 2020, 2:35:08 PM10/25/20
to sphinx...@googlegroups.com

Zoltan

unread,
Oct 25, 2020, 5:11:56 PM10/25/20
to sphinx-users
Thanks for both of you. I chose Eric's method. This is what I augmented my conf.py file, in case other readers will have a similar question:

def remove_copied_files(app, exception):
    os.remove('source/README.md')

def setup(app):
    app.connect('build-finished', remove_copied_files)
   # Rest of the function body ...

@Eric What is the advantage of your method compared to that of Takeshi Komiya? I can think of the following: one can use Python's cross-platform functions (os.remove in my case), so I do not have to edit both Makefile and make.bat). Other advantages?
Other question: how can I close this thread with a "Solved" tag?

Eric Larson

unread,
Oct 26, 2020, 7:49:54 AM10/26/20
to sphinx...@googlegroups.com
@Eric What is the advantage of your method compared to that of Takeshi Komiya? I can think of the following: one can use Python's cross-platform functions (os.remove in my case), so I do not have to edit both Makefile and make.bat). Other advantages?

I think it mostly comes down to whether you prefer to write your cleanup (or other) code in the Makefile or in Python.

Other question: how can I close this thread with a "Solved" tag?

No idea on this one.

Eric

Reply all
Reply to author
Forward
0 new messages