Pythonフレームワークの Bottleで180秒ででウェブサービス(ページ)を作りましょう!

はてなブックマーク - Pythonフレームワークの Bottleで180秒ででウェブサービス(ページ)を作りましょう!
LINEで送る
Pocket

目次

Pythonフレームワークの Bottleで180秒でウェブサービス(ページ)を作りましょう!

すみませんちょっと大げさかもしれません!笑

でも、それに近い時間で本当に速攻でサクッと作れます!

前の記事にも少しPythonフレームワークの Bottleを触れましたが

Pythonフレームワーク比較!2018版

Bottleがとても軽量で

全部で一つのファイルです!Pythonのバージョンも2も3も行けます!

今回demo用のはファイル二つしかありません!

Web用のPythonファイル(index.py)

from bottle import route, run,template,os
#
#ちょっとフォルダとファイルと出力してみます!(他のテストしたい処理でもいいです。)
#
def list_files(startpath):
    output_string=""
    for root, dirs, files in os.walk(startpath):
        level = root.replace(startpath, '').count(os.sep)
        indent = ' ' * 4 * (level)
        output_string=output_string+'{}{}/'.format(indent, os.path.basename(root))
        subindent = ' ' * 4 * (level + 1)
        for f in files:
            output_string=output_string+'{}{}'.format(subindent, f)
    return output_string
###########################################################
# WEB
###########################################################
@route('/yourpath')
def hello():
    output_string=list_files('/')
    return template("template",output_string=output_string)

run(host='0.0.0.0', port=5000)

次は

Templateファイル(template.html)

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4"
    crossorigin="anonymous">

  <title>Hello, World!</title>
</head>

<body>
  <div class='container'>
    <br>
    <div class='row'>
      <div class='col-lg-12 shadow-lg p-3 mb-5 bg-white rounded d-block'>
        <h1>Hello, World!</h1>
        <p>{{output_string}}</p>
      </div>
    </div>
  </div>

  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
    crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ"
    crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm"
    crossorigin="anonymous"></script>
</body>

</html>

そうしたら

サーバーを起動します!

python index.py

そうしましたら

ブラウザーで

http://localhost:5000/yourpath

を開いてみてください!

できました!

簡単すぎて、記事として、短すぎますが

でも本当に簡単ですから、仕方ありません!笑。

あとは、他のrouteが必要があれば、Pythonファイルに追加して行けば良いわけです。

少し規模が大きくなって行けば、当然180秒は足りません!人月でカウントすることになるでしょう!笑

Pythonのインストールに関しては、下の記事をご参考ください。

Ubuntu16.04LTSにpython3を入れる方法

 

Twitterもやっていますので

気軽にフォローどうぞ!

 

PythonのFlaskフレームワークでWebを作る!

はてなブックマーク - Pythonフレームワークの Bottleで180秒ででウェブサービス(ページ)を作りましょう!
LINEで送る
Pocket

Add a Comment

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

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close