├── .gitignore
├── LICENSE
├── README.md
├── plotapi
└── __init__.py
├── push.md
└── setup.py
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | pip-wheel-metadata/
24 | share/python-wheels/
25 | *.egg-info/
26 | .installed.cfg
27 | *.egg
28 | MANIFEST
29 |
30 | # PyInstaller
31 | # Usually these files are written by a python script from a template
32 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
33 | *.manifest
34 | *.spec
35 |
36 | # Installer logs
37 | pip-log.txt
38 | pip-delete-this-directory.txt
39 |
40 | # Unit test / coverage reports
41 | htmlcov/
42 | .tox/
43 | .nox/
44 | .coverage
45 | .coverage.*
46 | .cache
47 | nosetests.xml
48 | coverage.xml
49 | *.cover
50 | *.py,cover
51 | .hypothesis/
52 | .pytest_cache/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | target/
76 |
77 | # Jupyter Notebook
78 | .ipynb_checkpoints
79 |
80 | # IPython
81 | profile_default/
82 | ipython_config.py
83 |
84 | # pyenv
85 | .python-version
86 |
87 | # pipenv
88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
91 | # install all needed dependencies.
92 | #Pipfile.lock
93 |
94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95 | __pypackages__/
96 |
97 | # Celery stuff
98 | celerybeat-schedule
99 | celerybeat.pid
100 |
101 | # SageMath parsed files
102 | *.sage.py
103 |
104 | # Environments
105 | .env
106 | .venv
107 | env/
108 | venv/
109 | ENV/
110 | env.bak/
111 | venv.bak/
112 |
113 | # Spyder project settings
114 | .spyderproject
115 | .spyproject
116 |
117 | # Rope project settings
118 | .ropeproject
119 |
120 | # mkdocs documentation
121 | /site
122 |
123 | # mypy
124 | .mypy_cache/
125 | .dmypy.json
126 | dmypy.json
127 |
128 | # Pyre type checker
129 | .pyre/
130 |
131 | .DS_STORE
132 | .DS_STORE/
133 | plotapi_api_key
134 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022-2025 Dr. Shahin Rostami
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Engaging visualisations, made easy.
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | Key Features •
20 | Get Access •
21 | Installation •
22 | Usage •
23 | Docs
24 |
25 |
26 | 
27 |
28 | ## Key Features
29 |
30 | * **PlotAPI Chord** - Illustrate inter-relationships between data.
31 | * **PlotAPI SplitChord** - Illustrate inter-relationships between bipartite data.
32 | * **PlotAPI Sankey** - Illustrate the flow from one set of values to another.
33 | * **PlotAPI Terminus** - Illustrate distributing something out amongst recipients.
34 | * **PlotAPI Bar Fight** - A beautiful take on the classic Bar Chart Race.
35 | * **PlotAPI Pie Fight** - A beautiful take on the classic Pie Chart Race.
36 | * **PlotAPI Heat Map** - Beautiful and interactive heat maps.
37 | * **PlotAPI Line Fight** - A beautiful take on the classic Line Chart Race.
38 | * **PlotAPI Pareto Front** - Illustrate non-dominated (Pareto) fronts over time.
39 | * **PlotAPI Goal rush** - Illustrate progress toward a goal over time.
40 | * **Upcoming Visualisations** - Access to new visualisations as they are introduced.
41 | * **Supports Most Programming Languages** - Get started with any language able to make HTTP requests.
42 | * **First-Class Python Support** - Get started with pip install plotapi
43 | * **Jupyter Lab/Notebook + Google Colab Support** - Super-charge your notebooks with inline visualisations.
44 | * **Beautiful Themes & Fonts** - Select from pre-made beautiful themes, or add your own.
45 | * **Share or Embed Interactive HTML** - Download and embed your interactive visualisations.
46 | * **Upload and Share** - Upload and share your visualisations, privately or publicly.
47 | * **Render to PDF, SVG, or PNG** - Generate high-quality output ready for print.
48 | * **Record Animation to MP4 Video** - Create social media-ready animated visualisations.
49 |
50 | ## Get Access
51 |
52 | Visit [the website](https://plotapi.com/#pricing) to get access to PlotAPI.
53 |
54 | ## Installation
55 |
56 | Get up and running with Python with the following command.
57 |
58 | ```bash
59 | pip install plotapi
60 | ```
61 |
62 | ## Usage
63 |
64 | ### Example - Chord Pro
65 |
66 | https://user-images.githubusercontent.com/15690380/126084021-b008b256-2a31-4106-84af-42777ea480d9.mp4
67 |
68 | #### Created with PlotAPI
69 |
70 | ```python
71 | Chord(matrix, names).show()
72 | ```
73 |
74 | #### The data
75 |
76 | ```python
77 | matrix = [
78 | [0, 5, 6, 4, 7, 4],
79 | [5, 0, 5, 4, 6, 5],
80 | [6, 5, 0, 4, 5, 5],
81 | [4, 4, 4, 0, 5, 5],
82 | [7, 6, 5, 5, 0, 4],
83 | [4, 5, 5, 5, 4, 0],
84 | ]
85 |
86 | names = ["Action", "Adventure", "Comedy", "Drama", "Fantasy", "Thriller"]
87 | ```
88 |
89 | ### Example - Sankey Pro
90 |
91 | https://user-images.githubusercontent.com/15690380/126084745-712fd744-b626-429d-85f3-30b11979fe30.mp4
92 |
93 | #### Created with PlotAPI
94 |
95 | ```python
96 | Sankey(links).show()
97 | ```
98 |
99 | #### The data
100 |
101 | ```python
102 | links = [
103 | {"source":"Salary", "target":"Me", "value": 40000},
104 | {"source":"Freelance", "target":"Me", "value": 10000},
105 | {"source":"Dividends", "target":"Me", "value": 6000},
106 |
107 | {"source":"Me", "target":"Rent", "value": 1100*12},
108 | {"source":"Me", "target":"Food", "value": 300*12},
109 | {"source":"Me", "target":"Magazine Subscription", "value": 2000},
110 | {"source":"Me", "target":"Personal Tax", "value": 6000},
111 | {"source":"Me", "target":"Investments", "value": 30000},
112 |
113 | {"source":"Investments", "target":"Facebook", "value": 8000},
114 | {"source":"Investments", "target":"Amazon", "value": 7000},
115 | {"source":"Investments", "target":"Apple", "value": 6000},
116 | {"source":"Investments", "target":"Netflix", "value": 5000},
117 | {"source":"Investments", "target":"Google", "value": 4000},
118 | ]
119 | ```
120 |
121 | ## License
122 |
123 | MIT
124 |
125 | ---
126 |
127 | ## Prefer not to code? Try the App at PlotAPI.com
128 |
129 |
130 |
131 |
132 | > [plotapi.com](https://plotapi.com) ·
133 | > GitHub [@shahinrostami](https://github.com/shahinrostami) ·
134 | > Twitter [@shahinrostami](https://twitter.com/shahinrostami) ·
135 | > A [Polyra](https://polyra.com) innovation
136 |
--------------------------------------------------------------------------------
/plotapi/__init__.py:
--------------------------------------------------------------------------------
1 | """
2 | PlotAPI - Engaging visualisations, made easy.
3 |
4 | This package enables the generation of beautiful visualisations.
5 | They can be saved directly to HTML, PNG, PDF, and MP4 files.
6 | Supports Jupyter Notebooks, and uploading to PlotAPI.com.
7 |
8 | Copyright 2021-2024, Dr. Shahin Rostami
9 | https://shahinrostami.com
10 | https://plotapi.com
11 | https://github.com/shahinrostami/plotapi
12 | https://pypi.org/project/plotapi/
13 | """
14 | """ LICENSE
15 | MIT
16 | """
17 | import json
18 | import os
19 |
20 | import requests
21 |
22 | session = requests.Session()
23 | url = "https://plotapi.com"
24 | ssl_verification = True
25 | path = os.path.dirname(__file__)
26 |
27 | try:
28 | with open(f"{path}/plotapi_api_key", "r") as api_key_file:
29 | env_api_key = api_key_file.read().replace("\n", "")
30 |
31 | if env_api_key != None:
32 | session.auth = ("api_key", env_api_key)
33 | except:
34 | pass
35 |
36 |
37 | def content_encoding(raw_params):
38 | return raw_params, "identity"
39 |
40 |
41 | def api_key(key):
42 | global session
43 | session.auth = ("api_key", key)
44 |
45 | try:
46 | with open(f"{path}/plotapi_api_key", "w") as api_key_file:
47 | print(key, file=api_key_file)
48 |
49 | print(
50 | "Your PlotAPI API key has been saved in your local environment. You will not need to set it again."
51 | )
52 | except:
53 | pass
54 |
55 |
56 | class Visualisation:
57 | def api_key(key):
58 | api_key(key)
59 |
60 | def verify_ssl(truth):
61 | global ssl_verification
62 | ssl_verification = truth
63 |
64 | def set_license(username, password):
65 | global session
66 | session.auth = (username, password)
67 |
68 | def __init__(self, params, endpoint):
69 | self.params = params
70 | self.endpoint = endpoint
71 |
72 | def _ipython_display_(self):
73 | """Display the visualisation (as a side effect) within a notebook"""
74 | self.show()
75 |
76 | """PlotAPI Generation"""
77 |
78 | def get_html(self):
79 | """Generates the HTML using the Plotapi service."""
80 | params, directive = content_encoding(self.params)
81 | result = session.post(
82 | f"{url}/{self.endpoint}/",
83 | json=params,
84 | headers={"Content-Encoding": directive},
85 | verify=ssl_verification,
86 | )
87 |
88 | if result.status_code == 200:
89 | return result.text
90 | else:
91 | detail = json.loads(result.content.decode("utf8"))
92 | raise Exception(detail)
93 |
94 | def get_png(self, query_params=None):
95 | """Generates the PNG using the Plotapi service."""
96 | params, directive = content_encoding(self.params)
97 | result = session.post(
98 | f"{url}/{self.endpoint}/png",
99 | json=params,
100 | params=query_params,
101 | headers={"Content-Encoding": directive},
102 | verify=ssl_verification,
103 | )
104 |
105 | if result.status_code == 200:
106 | return result.content
107 | else:
108 | detail = json.loads(result.content.decode("utf8"))
109 | raise Exception(detail)
110 |
111 | def get_pdf(self, query_params=None):
112 | """Generates the PDF using the Plotapi service."""
113 | params, directive = content_encoding(self.params)
114 | result = session.post(
115 | f"{url}/{self.endpoint}/pdf",
116 | json=params,
117 | headers={"Content-Encoding": directive},
118 | verify=ssl_verification,
119 | )
120 |
121 | if result.status_code == 200:
122 | return result.content
123 | else:
124 | detail = json.loads(result.content.decode("utf8"))
125 | raise Exception(detail)
126 |
127 | def get_mp4(self, query_params=None):
128 | """Generates the MP4 using the Plotapi service."""
129 | params, directive = content_encoding(self.params)
130 | result = session.post(
131 | f"{url}/{self.endpoint}/mp4",
132 | json=params,
133 | headers={"Content-Encoding": directive},
134 | verify=ssl_verification,
135 | )
136 |
137 | if result.status_code == 200:
138 | return result.content
139 | else:
140 | detail = json.loads(result.content.decode("utf8"))
141 | raise Exception(detail)
142 |
143 | def get_svg(self, query_params=None):
144 | """Generates the SVG using the Plotapi service."""
145 | params, directive = content_encoding(self.params)
146 | result = session.post(
147 | f"{url}/{self.endpoint}/svg",
148 | json=params,
149 | headers={"Content-Encoding": directive},
150 | verify=ssl_verification,
151 | )
152 |
153 | if result.status_code == 200:
154 | return result.content
155 | else:
156 | detail = json.loads(result.content.decode("utf8"))
157 | raise Exception(detail)
158 |
159 | def to_string(self):
160 | """Outputs the generated HTML to as a string."""
161 | html = self.get_html()
162 | return html
163 |
164 | """Upload and share"""
165 |
166 | def upload(
167 | self,
168 | name="My First Upload",
169 | public=False,
170 | description="Uploaded from the API",
171 | custom_css="",
172 | maximized=False,
173 | full_width=False,
174 | ):
175 | """Upload visualization to PlotAPI.com."""
176 | self.params["plotapi_name"] = name
177 | self.params["plotapi_public"] = public
178 | self.params["plotapi_description"] = description
179 | self.params["plotapi_custom_css"] = custom_css
180 | self.params["plotapi_maximized"] = maximized
181 | self.params["plotapi_full_width"] = full_width
182 |
183 | params, directive = content_encoding(self.params)
184 | result = session.post(
185 | f"{url}/upload/{self.endpoint}",
186 | json=params,
187 | headers={"Content-Encoding": directive},
188 | verify=ssl_verification,
189 | )
190 |
191 | detail = json.loads(result.content.decode("utf8"))
192 |
193 | if result.status_code == 200:
194 | return detail
195 | else:
196 | raise Exception(detail)
197 |
198 | """File output"""
199 |
200 | def to_html(self, filename="out.html"):
201 | """Outputs the generated HTML to a HTML file."""
202 | html = self.get_html()
203 | file = open(filename, "w", encoding="utf-8")
204 | file.write(html)
205 | file.close()
206 |
207 | def to_png(self, filename="out.png", **kwargs):
208 | """Outputs the generated PNG to a file."""
209 | image = self.get_png(kwargs)
210 | file = open(filename, "wb")
211 | file.write(image)
212 | file.close()
213 |
214 | def to_pdf(self, filename="out.pdf", **kwargs):
215 | """Outputs the generated PDF to a file."""
216 | pdf = self.get_pdf(kwargs)
217 | file = open(filename, "wb")
218 | file.write(pdf)
219 | file.close()
220 |
221 | def to_mp4(self, filename="out.mp4", **kwargs):
222 | """Outputs the generated MP4 to a file."""
223 | pdf = self.get_mp4(kwargs)
224 | file = open(filename, "wb")
225 | file.write(pdf)
226 | file.close()
227 |
228 | def to_svg(self, filename="out.svg", **kwargs):
229 | """Outputs the generated SVG to a file."""
230 | svg = self.get_svg(kwargs)
231 | file = open(filename, "wb")
232 | file.write(svg)
233 | file.close()
234 |
235 | """Jupyter Lab output"""
236 |
237 | def show(self):
238 | """Outputs the generated HTML to a Jupyter Lab output cell."""
239 | from IPython.display import HTML, display
240 |
241 | html = self.get_html()
242 | display(HTML(html))
243 |
244 | def show_png(self, **kwargs):
245 | """Outputs the generated PNG to a Jupyter Lab output cell."""
246 | from IPython.display import Image, display
247 |
248 | image = self.get_png(kwargs)
249 | display(Image(image))
250 |
251 | def show_mp4(self, **kwargs):
252 | """Outputs the generated MP4 to a Jupyter Lab output cell."""
253 | from IPython.core.display import Video
254 | from IPython.display import display
255 |
256 | video = self.get_mp4(kwargs)
257 | display(
258 | Video(
259 | video,
260 | mimetype="video/mp4",
261 | html_attributes="autoplay controls",
262 | embed=True,
263 | )
264 | )
265 |
266 |
267 | class Chord(Visualisation):
268 | def __init__(self, matrix, names, **kwargs):
269 | params = kwargs
270 | params["matrix"] = matrix
271 | params["names"] = names
272 | endpoint = "chord"
273 | super().__init__(params, endpoint)
274 |
275 |
276 | class Sankey(Visualisation):
277 | def __init__(self, links, **kwargs):
278 | params = kwargs
279 | params["links"] = links
280 | endpoint = "sankey"
281 | super().__init__(params, endpoint)
282 |
283 |
284 | class Terminus(Visualisation):
285 | def __init__(self, links, **kwargs):
286 | params = kwargs
287 | params["links"] = links
288 | endpoint = "terminus"
289 | super().__init__(params, endpoint)
290 |
291 |
292 | class BarFight(Visualisation):
293 | def __init__(self, samples, **kwargs):
294 | params = kwargs
295 | params["samples"] = samples
296 | endpoint = "barfight"
297 | super().__init__(params, endpoint)
298 |
299 |
300 | class PieFight(Visualisation):
301 | def __init__(self, samples, **kwargs):
302 | params = kwargs
303 | params["samples"] = samples
304 | endpoint = "piefight"
305 | super().__init__(params, endpoint)
306 |
307 |
308 | class HeatMap(Visualisation):
309 | def __init__(self, matrix, **kwargs):
310 | params = kwargs
311 | params["matrix"] = matrix
312 | endpoint = "heatmap"
313 | super().__init__(params, endpoint)
314 |
315 |
316 | class LineFight(Visualisation):
317 | def __init__(self, samples, **kwargs):
318 | params = kwargs
319 | params["samples"] = samples
320 | endpoint = "linefight"
321 | super().__init__(params, endpoint)
322 |
323 |
324 | class ParetoFront(Visualisation):
325 | def __init__(self, samples, **kwargs):
326 | params = kwargs
327 | params["samples"] = samples
328 | endpoint = "paretofront"
329 | super().__init__(params, endpoint)
330 |
331 |
332 | class SplitChord(Visualisation):
333 | def __init__(self, links=None, nodes=None, **kwargs):
334 | params = kwargs
335 | params["links"] = links
336 | params["nodes"] = nodes
337 | endpoint = "splitchord"
338 | super().__init__(params, endpoint)
339 |
340 |
341 | class GoalRush(Visualisation):
342 | def __init__(self, progress=None, **kwargs):
343 | params = kwargs
344 | params["progress"] = progress
345 | endpoint = "goalrush"
346 | super().__init__(params, endpoint)
347 |
--------------------------------------------------------------------------------
/push.md:
--------------------------------------------------------------------------------
1 | python3 setup.py sdist bdist_wheel
2 |
3 | python3 -m twine upload --skip-existing dist/\*
4 |
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | from setuptools import setup
2 |
3 | with open("README.md", "r") as fh:
4 | long_description = fh.read()
5 |
6 | setup(
7 | name="plotapi",
8 | version="8.0.0",
9 | description="Engaging visualisations, made easy.",
10 | long_description=long_description,
11 | long_description_content_type="text/markdown",
12 | url="https://plotapi.com",
13 | author="Dr. Shahin Rostami",
14 | author_email="hello@shahinrostami.com",
15 | license="MIT",
16 | packages=["plotapi"],
17 | install_requires=["requests >=2.20.0, <3.0.0"],
18 | zip_safe=False,
19 | )
20 |
--------------------------------------------------------------------------------