• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

docinfosci / canvasxpress-python / b265905c-19cd-4eee-b5f3-3da898dd5b2f

08 Mar 2024 10:15PM UTC coverage: 81.057% (-3.2%) from 84.263%
b265905c-19cd-4eee-b5f3-3da898dd5b2f

push

circleci

docinfosci
pre-built streamlit assets for Python <= 3.6 builds

2024 of 2497 relevant lines covered (81.06%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

22.41
/canvasxpress/util/example/generate_tutorials.py
1
"""
2
This file can be executed to render the reproducible JSON files located at
3
`[project]/tutorials/reproducible_json/*.json` into tutorials for general use.
4
"""
5

6
import json
1✔
7
import os
1✔
8
from pathlib import Path
1✔
9
from typing import List
1✔
10

11
from canvasxpress.util.example.generator import (
1✔
12
    generate_canvasxpress_code_from_json_file,
13
)
14

15
JSON_DIR_PATH = f"{os.getcwd()}/../../../tutorials/reproducible_json/"
1✔
16
JUPYTER_TEMPLATE_PATH = (
1✔
17
    f"{os.getcwd()}/../../../canvasxpress/util/" f"example/template_tutorials.ipynb"
18
)
19
JUPYTER_EXAMPLES_DIR_PATH = (
1✔
20
    f"{os.getcwd()}/../../../tutorials/notebook/" f"cx_site_chart_examples/"
21
)
22

23

24
def get_json_file_paths() -> List[str]:
1✔
25
    """
26
    Returns a list of all reproducible JSON files tracked for tutorials.
27
    :returns: `list[str]`
28
        The file paths as a list of strings.
29
    """
30
    json_files = list()
×
31
    for file in os.listdir(JSON_DIR_PATH):
×
32
        if file.endswith(".json"):
×
33
            json_files.append(os.path.join(JSON_DIR_PATH, file))
×
34
    return sorted(json_files)
×
35

36

37
def get_type_from_filename(file_name: str) -> str:
1✔
38
    """
39
    Returns the type of chart from a reproducible JSON filename.
40
    :param file_name: `str`
41
        The name of the file without parent path.
42
    :returns: `str`
43
        The name of the chart (e.g., bar) or an empty string.
44
    """
45
    assembled_type = ""
×
46
    started_type = False
×
47
    for name_char in file_name.replace(".json", "")[::-1]:
×
48
        if not started_type and name_char.isnumeric():
×
49
            continue
×
50

51
        else:
52
            started_type = True
×
53
            assembled_type += name_char
×
54

55
    return assembled_type[::-1]
×
56

57

58
def get_index_from_filename(file_name: str) -> str:
1✔
59
    """
60
    Returns the index of chart from a reproducible JSON filename.
61
    :param file_name: `str`
62
        The name of the file without parent path.
63
    :returns: `str`
64
        The index of the chart (e.g., 1) or an empty string.
65
    """
66
    assembled_index = ""
×
67
    for name_char in file_name.replace(".json", "")[::-1]:
×
68
        if name_char.isnumeric():
×
69
            assembled_index += name_char
×
70

71
        else:
72
            break
×
73

74
    return assembled_index[::-1]
×
75

76

77
def create_jupyer_template_text(
1✔
78
    chart_type: str, chart_index: str, chart_code: str
79
) -> str:
80
    """
81
    Generates the text for a Jupyter Notebook example given a chart's type,
82
    index, and code.
83
    :param: chart_type: `str`
84
        The type text (e.g., bar) for the chart.
85
    :param chart_index: `str`
86
        The index text (e.g., 1) for the chart.
87
    :param chart_code: `str`
88
        The chart source code.
89
    :returns: `str`
90
        The text for the full example and instruction.
91
    """
92
    with open(JUPYTER_TEMPLATE_PATH, "r") as template_file:
×
93
        example_text = template_file.read()
×
94
        example_text = example_text.replace("@type@", chart_type)
×
95
        example_text = example_text.replace("@index@", chart_index)
×
96

97
        ipython_json = json.loads(example_text)
×
98
        for line in chart_code.splitlines():
×
99
            candidate = line
×
100

101
            # Convert render statement to explicit output
102
            if "display.render()" in candidate:
×
103
                candidate = candidate.replace(
×
104
                    "display.render()",
105
                    f'display.render(output_file="{chart_type}_{chart_index}.html")',
106
                )
107

108
            # Add the source line to the document
109
            ipython_json["cells"][1]["source"].append(candidate + "\n")
×
110

111
        ipython_text = json.dumps(ipython_json)
×
112
        return ipython_text
×
113

114

115
if __name__ == "__main__":
1✔
116
    json_paths = get_json_file_paths()
×
117
    for json_path in json_paths:
×
118
        try:
×
119
            json_name = Path(json_path).name
×
120

121
            chart_type = get_type_from_filename(json_name)
×
122
            chart_index = get_index_from_filename(json_name)
×
123

124
            jupyter_notebook_content = create_jupyer_template_text(
×
125
                chart_type,
126
                chart_index,
127
                generate_canvasxpress_code_from_json_file(
128
                    json_path, document_jupyter_render=True
129
                ),
130
            )
131

132
            example_file_name = f"{chart_type}_{chart_index}.ipynb"
×
133
            example_file_path = str(
×
134
                Path(JUPYTER_EXAMPLES_DIR_PATH).joinpath(example_file_name)
135
            )
136
            with open(example_file_path, "w") as example_file:
×
137
                example_file.write(jupyter_notebook_content)
×
138

139
        except Exception as e:
×
140
            print(f"Cannot process file: {json_path}")
×
141
            print(f"Exception: {e}")
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc