File: generate_readme.py

package info (click to toggle)
python-imgviz 1.2.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,268 kB
  • sloc: python: 3,032; makefile: 15
file content (136 lines) | stat: -rwxr-xr-x 3,503 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
132
133
134
135
136
#!/usr/bin/env python

from __future__ import print_function

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 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(
            (
                '<pre><a href="{}">{}</a></pre>'.format(py_file, py_file),
                '<img src="{}" width="{}%" />'.format(img_file, width),
            )
        )
    examples = tabulate(examples)

    dependencies = []
    with open("requirements.txt") as f:
        for req in f:
            if req.startswith("#"):
                continue
            req = req.strip()
            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>
  <a href="https://imgviz.readthedocs.io/en/latest/?badge=latest"><img src="https://readthedocs.org/projects/imgviz/badge/?version=latest" alt="Documentation Status" /></a>
</div>

<div align="center">
  <a href="https://imgviz.readthedocs.io/en/latest/?badge=latest"><b>Documentation</b></a> |
  <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=".readme/getting_started.jpg" width="75%">
</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()