sphinx快速教程#
1. 创建项目#
1.1. create python venv#
python3 -m venv venv
source venv/bin/activate
1.2. install sphinx#
pip install sphinx
1.3. create project#
sphinx-quickstart [project_name]
1.4. build#
cd project_name
make html
1.5. deploy#
cp -r build/html/* /path/to/deploy/dir
2. 自定义主题#
2.1. install theme#
pip install sphinx_rtd_theme
pip install pydata_sphinx_theme
pip install sphinx_book_theme
pip install sphinx_press_theme
2.2. enable theme#
vi conf.py
html_theme = "pydata_sphinx_theme"
#html_theme = 'alabaster'
#html_theme = 'sphinx_rtd_theme'
#html_theme = 'press'
#html_theme = 'sphinx_book_theme'
#html_theme = 'furo'
3. markdown support#
pip install myst-parser
vi conf.py
extensions = ['myst_parser']
3.1. myst markdown useful extensions Latex support#
dollarmath amsmath
myst_enable_extensions = [
"amsmath",
"attrs_inline",
"colon_fence",
"deflist",
"dollarmath",
"fieldlist",
"html_admonition",
"html_image",
# "linkify",
"replacements",
"smartquotes",
"strikethrough",
"substitution",
"tasklist",
]
3.2 markdown tabs support#
pip install sphinx_tabs
vi conf.py
extensions = ['myst_parser','sphinx_tabs.tabs']
::::{tabs}
:::{tab} python
``` print('hello')```
:::
:::{tab} shell
```echo hello```
:::
::::
效果:
print('hello')
echo hello
3.3. markdown Admonitions types#
attention, caution, danger, error, hint, important, note, seealso, tip, warning
:::{note}
**提示**
加上上面这行就可以写小技巧了
:::
效果:
备注
提示 加上上面这行就可以写小技巧了
其他样式 attention, caution, danger, error, hint, important, note, seealso, tip, warning
注意
attention
小心
caution
危险
danger
错误
error
提示
hint
重要
important
备注
note
参见
seealso
小技巧
tip
警告
warning
3.3.1 admonition options#
class: A space-separated list of CSS classes to add to the admonition, conforming to the identifier normalization rules. name: A reference target for the admonition (see cross-referencing).
:::{tip}
:class: myclass1 myclass2
:name: a-tip-reference
Let's give readers a helpful hint!
:::
[Reference to my tip](#a-tip-reference)
效果:
小技巧
Let’s give readers a helpful hint!
3.3.2 Collapsible admonitions#
pip install sphinx_togglebutton
extensions = ['myst_parser', 'sphinx_togglebutton']
:::{note}
:class: dropdown
This admonition has been collapsed,
meaning you can add longer form content here,
without it taking up too much space on the page.
:::
效果:
备注
This admonition has been collapsed, meaning you can add longer form content here, without it taking up too much space on the page.
3.3.3 additional admonition types#
:::{versionadded} 1.2.3
Explanation of the new feature.
:::
:::{versionchanged} 1.2.3
Explanation of the change.
:::
:::{deprecated} 1.2.3
Explanation of the deprecation.
:::
Added in version 1.2.3: Explanation of the new feature.
在 1.2.3 版本发生变更: Explanation of the change.
自 1.2.3 版本弃用: Explanation of the deprecation.
3.3.4 custom admonitions with Markdown#
:::{admonition} My custom title with *Markdown*!
:class: tip
This is a custom title for a tip admonition.
:::
My custom title with Markdown!
This is a custom title for a tip admonition.
3.3.5 Other Containers (grids, tabs, cards, etc.)#
pip install sphinx_design
extensions = ['myst_parser',"sphinx_design"]
:::bg-primary
This is a container with a custom CSS class.
- It can contain multiple blocks
:::
This is a container with a custom CSS class.
It can contain multiple blocks
:::{card} Card Title
Header
^^^
Card content
+++
Footer
:::
Header
Card content
::::{tab-set}
:::{tab-item} Label1
Content 1
:::
:::{tab-item} Label2
Content 2
:::
::::
Content 1
Content 2
4. theme options: Logo 顶部外链 图标链接 版本切换 顶部公告#
html_theme_options = {
# 控制侧边栏展开的层级深度,根据需要调整
'navigation_depth': 4,
# 版本切换
"switcher": {
"json_url": "https://doc.thereisno.top/_static/version.json",
"version_match": "0.1",
},
"navbar_start": ["navbar-logo","version-switcher"],
#"navbar_center": ["navbar-nav"],
#"navbar_end": ["navbar-icon-links"],
#"navbar_persistent": ["search-button"],
#"secondary_sidebar_items": ["page-toc", "edit-this-page", "sourcelink"],
# 顶部公告
"announcement": "腾讯云主机特惠,2核2G仅68元/年, <a href='https://curl.qcloud.com/xj2YW0Z2'>去看看</a>",
#"content_footer_items": ["last-updated"],
"logo": {
# In a left-to-right context, screen readers will read the alt text
# first, then the text, so this example will be read as "P-G-G-P-Y
# (short pause) Home A pretty good geometry package"
"alt_text": "Home",
"text": "葵花宝典🌻",
"image_light": "_static/logo_w.png",
"image_dark": "_static/logo_b.png",
},
#"primary_sidebar_end": ["indices.html", "sidebar-ethical-ads.html"]
# 顶部链接
"external_links": [
{"name": "博客", "url": "https://thereisno.top"},
],
# 右侧图标链接
#"github_url": 'https://github.com/xuenhua',
"icon_links": [
{
"name": "Github",
"url": "https://github.com/xuenhua",
"icon": "fa-brands fa-github",
},
{
"name": "Weibo",
"url": "https://weibo.com/xuenhua",
"icon": "fa-brands fa-weibo",
# The default for `type` is `fontawesome`, so it is not required in the above examples
},
{
"name": "Blog",
"url": "https://thereisno.top",
"icon": "fa-brands fa-wordpress",
#"type": "local",
},
#{
# "name": "Gitee",
# "url": "https://gitee.com/xuenhua",
# "icon": "fa-brands fa-git-alt",
# # The default for `type` is `fontawesome`, so it is not required in the above examples
#},
{
"name": "Page",
"url": "https://xuenhua.github.io/xuenhua",
"icon": "fa-brands fa-tencent-weibo",
# The default for `type` is `fontawesome`, so it is not required in the above examples
},
{
"name": "TikTok",
"url": "https://v.douyin.com/bYO_tUbvWGc/",
"icon": "fa-brands fa-tiktok",
# The default for `type` is `fontawesome`, so it is not required in the above examples
},
],
}
5. 是否显示markdow rst 源码按钮#
html_show_sourcelink = False
7. 显示标题级别#
# 配置 MyST 解析器(确保每个文件仅显示顶级标题)
# myst_heading_anchors = 1 # 仅生成顶级标题的锚点
8. Grid#
# xs sm md lg 屏幕大小超小屏(手机) 小屏幕(平板) 中等屏幕(桌面显示器) 大屏幕(大桌面)
````{grid} 1 1 2 2
:gutter: 3
```{grid-item-card}
User Interfaces
^^^^^^
* [JupyterLab](https://github.com/jupyterlab/jupyterlab)
* [Jupyter Notebook](https://jupyter-notebook.readthedocs.io/en/latest/)```
```{grid-item-card}
JupyterHub
^^^^^^
* [JupyterHub](https://jupyterhub.readthedocs.io/en/latest)
* [Configurable HTTP proxy](https://github.com/jupyterhub/configurable-http-proxy)```
```{grid-item-card}
Working with Notebooks
^^^^^^
* [nbclient](https://nbclient.readthedocs.io/en/latest/) - execution
* [nbconvert](https://nbconvert.readthedocs.io/en/latest/) - conversion```
```{grid-item-card}
IPython
^^^^^^
* [IPython](https://ipython.readthedocs.io/en/stable/)
* [ipykernel](https://ipython.readthedocs.io/en/stable/)``` ````
User Interfaces
JupyterHub
9. csv-table#
```{csv-table}
:header: Site, Description
:widths: 18, 35
[Jupyter website](https://jupyter.org), Keep up to date on Jupyter
[IPython website](https://ipython.org), Learn more about IPython
[Jupyter Discourse forum](https://discourse.jupyter.org/), Start here for help and support questions```
Site |
Description |
---|---|
Keep up to date on Jupyter |
|
Learn more about IPython |
|
Start here for help and support questions |
10. 插入目录#
(ref-content1)=
```{toctree}
:maxdepth: 2
../demo```
11. 索引#
- {ref}`genindex`
- {ref}`search`
12. 从代码生成文档#
#从代码生成文档
sphinx-apidoc -o source/testdoc/ ../../code_path/
vi conf.py
# 从源码生成文档,build时源码和文档关联
import os
import sys
sys.path.insert(0, os.path.abspath('../../../code_path/')) # 将项目根目录添加到模块查找路径中去
extensions = [
#生成文档
'sphinx.ext.autodoc',
#显示源码
'sphinx.ext.viewcode',
]