ランダマイザー作ってみた
〜Python製〜

Python製ランダマイザーのコードは以下です。
HTMLのテンプレートをスクリプトのファイルに
含めることができなかったので、
ファイルが3つになってます。
まずはスクリプトのファイルから。


# coding: utf-8
import random
from flask import Flask, render_template, redirect
app = Flask(__name__)

@app.route("/python/rand")
def helo():
    return redirect("/python/rand/",code=301)
@app.route("/python/rand/")
def index():
    return render_template('index.html')

@app.errorhandler(404)
def error_handler(error):
    return render_template('404.html')

if __name__ == "__main__":
    app.run(host='0.0.0.0')

関数名とか適当ですね。
続いてindex.html


<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>
ランダマイザー
</title>
</head>
<body>
<center>
<span style="font-size: 5vh;">
 {%set ab=['a','b']%}
 {%for num in range(1,11):%}
 {%set number_padded = '{0:02d}'.format(num)%}
 {%set rand=range(0, 2) | random %}
 {{ number_padded }} : {{ab[rand]}} <br />
 {%endfor%}
<input type="button" value="もう1回" onclick="window.location.reload(true);" style="font-size: 3vh; WIDTH:25vw ; HEIGHT: 10vh"/>
</span>
</center>
</body>
</html>

rangeを指定するときは欲しい値の最大値+1になってるのが特徴かな?
あとは404のエラーページ。


<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>
そのURLはコンテンツが存在しません。
</title>
</head>
<body>
<center>
<span style="font-size: 3vh;">
そのURLはコンテンツが存在しません。<br />
<a href="/">トップページに戻ってみてください。</a><br />
</span>
</center>
</body>
</html>

普通にhtmlを返してるだけです。

テンプレートとかもっと他に書き方があったかもしれないけど、
今回は見つけられなかったのでこんな感じです。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です