1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PythonからMermaidの図表を取得する

Posted at

メモ

  • 実際にはPythonで画像を生成するのではなく、WEBサービスを利用する
  • Pythonはただリクエストパラメータを生成するだけ
  • 取得すると書いたが、下記PythonコードではURLの生成まで
  • ファイル形式はSVGかJPEG
  • リクエストデータ量を減らしたい場合は圧縮のURLを利用する


ソースコード

code = """
gantt
  title A Gantt Diagram
  dateFormat  YYYY-MM-DD
  section Section
  A task           :a1, 2014-01-01, 30d
  Another task     :after a1  , 20d
  section Another
  Task in sec      :2014-01-12  , 12d
  another task      : 24d
""".strip()

state = {
    'code': code,
}
ascii_bytes = json.dumps(state).encode("ascii")

# base64
base64_string = base64.b64encode(ascii_bytes).decode("ascii") 
print(f"https://mermaid.ink/svg/base64:{base64_string}")
print(f"https://mermaid.ink/img/base64:{base64_string}")

# pako(圧縮)
compressed = zlib.compress(ascii_bytes)
base64_string = base64.urlsafe_b64encode(compressed).decode('ascii')
print(f'https://mermaid.ink/svg/pako:{base64_string}')
print(f'https://mermaid.ink/img/pako:{base64_string}')
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?