Scripts that can be used when using bottle in Python

When using bottle in Python, it's a hassle to write various things from the beginning. So I decided to write the minimum necessary contents so that I can use it immediately.

main_app.py



#!/user/bin/env python
# -*- coding: utf-8 -*-
from bottle import route, run, template, request, static_file, url, get, post, response, error
import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)

@route("/")
def html_index():
	return template("index")

@route("/static/<filepath:path>", name="static_file")
def static(filepath):	
	return static_file(filepath, root="./static")


@get("/login")
def login():
    return """
        <form action="/login" method="post">
            Username: <input name="username" type="text" />
            Password: <input name="password" type="password" />
            <input value="Login" type="submit" />
        </form>
    """
@route("/login", method="POST")
def do_login():
    username = request.forms.get("username")
    password = request.forms.get("password")
    if check_login(username, password):
        response.set_cookie("account", username, secret="some-secret-key")
        return template("index", name=username)
    else:
        return "<p>Failed !</p>"
def check_login(username, password):
  if username == "admin" and password=="password":
    return True
  else:
    return False


@error(404)
def error404(error):
	return template("404")

run(host="localhost", port=8000, debug=True, reloader=True)

Like this, I will write the login function itself from the beginning. Only 404 in error is written from the beginning, and 500 error is added according to when used.

myapp - bottle.py
		main_app.py
		
		/static
			- /css
			- /img
			- /js
		
		/views 
			- index.html
			- 404.html

I think you should create a directory like this. By the way, the views template file does not have to have a .tpl extension.

Recommended Posts

Scripts that can be used when using bottle in Python
Summary of statistical data analysis methods using Python that can be used in business
Python standard input summary that can be used in competition pro
Functions that can be used in for statements
Basic algorithms that can be used in competition pros
Japanese can be used with Python in Docker environment
Python knowledge notes that can be used with AtCoder
ANTs image registration that can be used in 5 minutes
Can be used in competition pros! Python standard library
I made a familiar function that can be used in statistics with Python
[Python3] Code that can be used when you want to cut out an image in a specific size
Precautions when using pit in Python
[Python] Variadic arguments can be used when unpacking iterable elements
Goroutine (parallel control) that can be used in the field
[Python] Be careful when using print
Goroutine that can be used in the field (errgroup.Group edition)
When using regular expressions in Python
33 strings that should not be used as variable names in python
New features in Python 3.9 (1)-Union operators can be used in dictionary types
Python standard module that can be used on the command line
Easy padding of data that can be used in natural language processing
I created a template for a Python project that can be used universally
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
++ and-cannot be used for increment / decrement in python
File types that can be used with Go
Building Sphinx that can be written in Markdown
I want to create a priority queue that can be updated in Python (2.7)
If "can not be used when making a PIE object" appears in make
A personal memo of Pandas related operations that can be used in practice
Easy program installer and automatic program updater that can be used in any language
Summary of scikit-learn data sources that can be used when writing analysis articles
How to install a Python library that can be used by pharmaceutical companies
If you want to make a Windows application (exe) that can be actually used now using only Python
[Python3] Code that can be used when you want to change the extension of an image at once
Python note: When the pip command cannot be used
Initial settings when using the foursquare API in python
[Memorandum] Japanese keys cannot be used in python string.Template.substitute
Causes when python version cannot be changed using pyenv
What's in that variable (when running a Python script)
Operators ++,-cannot be used in python (difference from php)
How to exit when using Python in Terminal (Mac)
Non-linear simultaneous equations can be easily solved in Python.
[Redash] Standard library cannot be used in python function
Set up an FTP server that can be created and destroyed immediately (in Python)
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
A mechanism to call a Ruby method from Python that can be done in 200 lines
List of tools that can be used to easily try sentiment analysis of Japanese sentences in Python (try with google colab)
Dynamically import scripts in Python
Processing of python3 that seems to be usable in paiza
Install Mecab and CaboCha on ubuntu16.04LTS so that it can be used from python3 series
Using verticalenv in shell scripts
How to set up a simple SMTP server that can be tested locally in Python
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
Master the type in Python? (When should type check be done)
A memo when creating a directed graph using Graphviz in Python
Text analysis that can be done in 5 minutes [Word Cloud]
[Django] Field names, user registration, and login methods that can be used in the User model
Get Python scripts to run quickly in Cloud Run using responder
Things to keep in mind when using Python with AtCoder
Precautions that must be understood when building a PYTHON environment
Things to keep in mind when using cgi with python.