I was frustrated by Django and wanted a framework that I could understand for the time being. So I arrived at the bottle. A framework made up of only a script file called bottle.py. A simple web application will make you feel like you can run it right away.
That's why I tried sending a tweet with a browser.
index.py
#!/user/bin/env python
# -*- coding: utf-8 -*-
from bottle import route, run, template, request
from requests_oauthlib import OAuth1Session
import json
import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
@route("/")
def post():
return template("post")
@route("/show", method="GET")
def msg():
msg = request.query.msg
C_KEY = "****************************"
C_SECRET = "****************************"
A_KEY = "****************************"
A_SECRET = "****************************"
url = "https://api.twitter.com/1.1/statuses/update.json"
params = {"status": msg,"lang": "ja"}
tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
req = tw.post(url, params = params)
return template("show", msg=msg)
run(host="localhost", port="8000", debug=True, reloader=True)
Next, create a template. Create a folder called views in the same directory as bottle.py and index.py, Save the following HTML file as post.html.
<!DOCTYPE html>
<html lang=ja>
<head>
<meta charset="UTF-8">
<title>Let's tweet</title>
</head>
<body>
<h1>Enter a tweet</h1>
<form method="GET" action="/show">
<p>Tweet:
<input type="text" name="msg"></p>
<input type="submit" value="Send">
</body>
</html>
Since the tweet completion screen will be created in the same way, save it as show.html in the same views folder as the above HTML file.
<!DOCTYPE html>
<html lang=ja>
<head>
<meta charset="UTF-8">
<title>Tweet result</title>
</head>
<body>
<h1>Tweet completed!</h1>
<p>Your Tweet:{{msg}} <p>
<a href="/">Back</a>
</p>
</body>
</html>
Run index.py from the terminal http://127.0.0.1:8000/ Access to. That's it.
By the way, in the bottle template,
% if ~:
Or
<%
for i in s:
print i
%>
It is possible to write Python code between%.
I also wrote a Django version. http://qiita.com/Gen6/items/735245423b65698428be