File: generate_readme.py

package info (click to toggle)
python-imgviz 1.7.6%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,056 kB
  • sloc: python: 3,131; makefile: 25
file content (131 lines) | stat: -rwxr-xr-x 3,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python

import glob
import os.path as osp

import PIL.Image


def tabulate(rows):
    html = "<table>"
    for row in rows:
        html += "\n\t<tr>"
        for col in row:
            html += "\n\t\t<td>{}</td>".format(col)
        html += "\n\t</tr>"
    html += "\n</table>"
    return html


def _get_github_image_url(relpath: str) -> str:
    return f"https://github.com/wkentaro/imgviz/raw/main/{relpath}"


def main():
    examples = []
    for py_file in sorted(glob.glob("examples/*.py")):
        img_file = osp.splitext(osp.basename(py_file))[0] + ".jpg"
        img_file = osp.join("examples/.readme", img_file)
        if not osp.exists(img_file):
            continue
        img = PIL.Image.open(img_file)
        width = 20.0 / img.height * img.width
        examples.append(
            (
                f'<pre><a href="{py_file}">{py_file}</a></pre>',
                f'<img src="{_get_github_image_url(relpath=img_file)}" width="{width}%" />',  # NOQA: E501
            )
        )
    examples = tabulate(examples)

    # TODO: read from pyproject.toml
    dependencies = []
    for req in ["matplotlib", "numpy", "Pillow>=5.3.0", "PyYAML"]:
        pkg = req
        for sep in "<=>":
            pkg = pkg.split(sep)[0]
        dependencies.append("- [{0}](https://pypi.org/project/{1})".format(req, pkg))
    dependencies = "\n".join(dependencies)

    py_file = "getting_started.py"
    with open(py_file) as f:
        active = False
        lines = []
        for line in f:
            if line == "# GETTING_STARTED {{\n":
                active = True
                continue
            elif line == "# }} GETTING_STARTED\n":
                active = False
                continue
            if active:
                lines.append(line)
    getting_started = "".join(lines)

    README = """\
<!-- DO NOT EDIT THIS FILE MANUALLY. This file is generated by generate_readme.py. -->

<h1 align="center">
  imgviz
</h1>

<h4 align="center">
  Image Visualization Tools
</h4>

<div align="center">
  <a href="https://pypi.python.org/pypi/imgviz"><img src="https://img.shields.io/pypi/v/imgviz.svg"></a>
  <a href="https://pypi.org/project/imgviz"><img src="https://img.shields.io/pypi/pyversions/imgviz.svg"></a>
  <a href="https://github.com/wkentaro/imgviz/actions"><img src="https://github.com/wkentaro/imgviz/workflows/ci/badge.svg"></a>
</div>

<div align="center">
  <a href="#installation"><b>Installation</b></a> |
  <a href="#getting-started"><b>Getting Started</b></a> |
  <a href="#examples"><b>Examples</b></a> |
  <a href="https://github.com/wkentaro/imgviz-cpp"><b>C++ Version</b></a>
</div>

<br/>

<div align="center">
  <img src="https://github.com/wkentaro/imgviz/raw/main/.readme/getting_started.jpg" width="95%">
</div>

## Installation

```bash
pip install imgviz

# there are optional dependencies like skimage, below installs all.
pip install imgviz[all]
```


## Dependencies

{dependencies}

## Getting Started

```python
# getting_started.py

{getting_started}```

## [Examples](examples)

{examples}
"""  # NOQA

    README = README.format(
        getting_started=getting_started,
        dependencies=dependencies,
        examples=examples,
    )

    print(README, end="")


if __name__ == "__main__":
    main()