python bottle でアプリを作り始める時のテンプレコード ( +Vue CDN と組み合わせる場合)

投稿者: ytyng 1年, 6ヶ月 前

bottle で簡単な HTML アプリを作る時のテンプレートのコードです。

main.py

from bottle import route, run, view, debug


@route('/')
@view('index')
def index():
    return {}


if __name__ == '__main__':
    debug(True)
    run(host='127.0.0.1', port=8080, reloader=True)

views/base.tpl

<html lang="ja">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
        rel="stylesheet"
        integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
        crossorigin="anonymous">
  <title>{{ title or 'default title' }}</title>
</head>
<body>
<header class="bg-dark">
  <div class="p-2">
    <a href="/" class="text-white h3 m-0 text-decoration-none">
    Site Title
    </a>
  </div>
</header>
{{!base}}
</body>
</html>

views/index.tpl

% rebase('base.tpl', title='Page Title')
<p>This is index content</p>

Vue CDN と組み合わせる場合

Bottle と Vue CDN を組み合わせると、単機能アプリであればかなり早く、少ないコードで構築できる。

自分しか使わないツールであったり、限られたチームでしか使わない LAN 内のアプリであれば十分。

main.py

from bottle import route, run, view, debug, StplParser


@route('/')
@view('index')
def index():
    return {}


if __name__ == '__main__':
    debug(True)
    StplParser.default_syntax = '<% %> % [[ ]]'
    run(host='127.0.0.1', port=8080, reloader=True)

views/base.tpl

<html lang="ja">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
        rel="stylesheet"
        integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
        crossorigin="anonymous">
  <title>[[ title or 'default title' ]]</title>
  <style>
    [v-cloak] {
      display: none;
    }
  </style>
</head>
<body>
<header class="bg-dark">
  <div class="p-2">
    <a href="/" class="text-white h3 m-0 text-decoration-none">
    Site Title
    </a>
  </div>
</header>
[[!base]]
</body>
</html>

views/index.tpl

% rebase('base.tpl', title='Page Title')

<div id="app" v-cloak>
  <p>{{ message }}</p>
</div>

<script src="https://unpkg.com/vue@next"></script>
<script>
  const app = Vue.createApp({
    data() {
      return {
         message: '',
      };
    },
    methods: {
      ...
    }
  })
  app.mount('#app')
</script>

参考ページ

Bottle ドキュメント https://bottlepy.org/docs/dev/index.html

現在未評価

コメント

アーカイブ

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011