Template Code for Starting an App with Python Bottle (and Combining with Vue CDN)

2022-10-10 10:47 (2 years ago) ytyng

Here is a template code for creating a simple HTML app with Bottle.

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="en">
<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>

When combining with Vue CDN

When combining Bottle with Vue CDN, you can build a simple feature app very quickly and with minimal code.

This is sufficient if it's a tool only used by yourself or an app used within a limited team in a LAN environment.

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="en">
<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>

Reference Page

Bottle Documentation: https://bottlepy.org/docs/dev/index.html

Currently unrated

Comments

Archive

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