Created
October 17, 2019 02:20
-
-
Save umanoda/f670b4fb061d5921cc93d01c3c61a884 to your computer and use it in GitHub Desktop.
use cacti and python-pptx in GCF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import math | |
import os | |
import tempfile | |
from typing import Any | |
import cairo | |
from google.cloud import storage | |
from pptx import Presentation | |
def __make_tempfile(prefix: str, ext: str) -> str: | |
_, file_path = tempfile.mkstemp( | |
prefix=f"{prefix}.{datetime.datetime.today().strftime('%Y%m%d_%H%M%S')}.", | |
suffix=ext, | |
) | |
return file_path | |
def __copy_to_gcs(file_path: str): | |
bucket = storage.bucket.Bucket(storage.Client(), "hoshino-test-242001.appspot.com") | |
blob = bucket.blob(os.path.basename(file_path)) | |
blob.upload_from_filename(file_path) | |
print(blob.public_url) | |
def __create_pptx(file_path: str): | |
prs = Presentation() | |
title_slide_layout = prs.slide_layouts[0] | |
slide = prs.slides.add_slide(title_slide_layout) | |
title = slide.shapes.title | |
subtitle = slide.placeholders[1] | |
title.text = "Hello, World!" | |
subtitle.text = "python-pptx was here!" | |
prs.save(file_path) | |
__copy_to_gcs(file_path) | |
def __create_svg(file_path: str): | |
print(f"output svg: {file_path}") | |
with cairo.SVGSurface(file_path, 200, 200) as surface: | |
context = cairo.Context(surface) | |
x, y, x1, y1 = 0.1, 0.5, 0.4, 0.9 | |
x2, y2, x3, y3 = 0.6, 0.1, 0.9, 0.5 | |
context.scale(200, 200) | |
context.set_line_width(0.04) | |
context.move_to(x, y) | |
context.curve_to(x1, y1, x2, y2, x3, y3) | |
context.stroke() | |
context.set_source_rgba(1, 0.2, 0.2, 0.6) | |
context.set_line_width(0.02) | |
context.move_to(x, y) | |
context.line_to(x1, y1) | |
context.move_to(x2, y2) | |
context.line_to(x3, y3) | |
context.stroke() | |
__copy_to_gcs(file_path) | |
def __create_png(file_path: str): | |
WIDTH, HEIGHT = 256, 256 | |
print(f"output png: {file_path}") | |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) | |
ctx = cairo.Context(surface) | |
ctx.scale(WIDTH, HEIGHT) # Normalizing the canvas | |
pat = cairo.LinearGradient(0.0, 0.0, 0.0, 1.0) | |
pat.add_color_stop_rgba(1, 0.7, 0, 0, 0.5) # First stop, 50% opacity | |
pat.add_color_stop_rgba(0, 0.9, 0.7, 0.2, 1) # Last stop, 100% opacity | |
ctx.rectangle(0, 0, 1, 1) # Rectangle(x0, y0, x1, y1) | |
ctx.set_source(pat) | |
ctx.fill() | |
ctx.translate(0.1, 0.1) # Changing the current transformation matrix | |
ctx.move_to(0, 0) | |
# Arc(cx, cy, radius, start_angle, stop_angle) | |
ctx.arc(0.2, 0.1, 0.1, -math.pi / 2, 0) | |
ctx.line_to(0.5, 0.1) # Line to (x,y) | |
# Curve(x1, y1, x2, y2, x3, y3) | |
ctx.curve_to(0.5, 0.2, 0.5, 0.4, 0.2, 0.8) | |
ctx.close_path() | |
ctx.set_source_rgb(0.3, 0.2, 0.5) # Solid color | |
ctx.set_line_width(0.02) | |
ctx.stroke() | |
surface.write_to_png(file_path) # Output to PNG | |
__copy_to_gcs(file_path) | |
def create_svg(request: Any): | |
__create_svg(__make_tempfile("example", ".svg")) | |
__create_png(__make_tempfile("example", ".png")) | |
__create_pptx(__make_tempfile("example", ".pptx")) | |
return "ok" | |
if __name__ == "__main__": | |
create_svg(dict()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[source]] | |
name = "pypi" | |
url = "https://pypi.org/simple" | |
verify_ssl = true | |
[dev-packages] | |
pylint = "*" | |
mypy = "*" | |
flake8 = "*" | |
[packages] | |
pycairo = "*" | |
google-cloud-storage = "*" | |
python-pptx = "*" | |
[requires] | |
python_version = "3.7" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
GCFにdeployして動作させたら、svg, png, pptxが作れた