Browse Source

flask complete

tobby48 6 years ago
parent
commit
8ac497aed8
19 changed files with 244 additions and 10 deletions
  1. 1
    0
      .settings/org.eclipse.core.resources.prefs
  2. 18
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/cookie.py
  3. 14
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/error.py
  4. 29
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/jinja-quiz.py
  5. 9
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/jinja_comment.py
  6. 10
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/jinja_for.py
  7. 9
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/jinja_if.py
  8. 9
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/jinja_variables.py
  9. 12
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/naver.py
  10. 2
    2
      src/main/python/kr/co/swh/lecture/opensource/flask/restful-quiz.py
  11. 20
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/templates/comment.html
  12. 8
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/templates/cookie_add.html
  13. 7
    7
      src/main/python/kr/co/swh/lecture/opensource/flask/templates/cookie_hello.html
  14. 18
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/templates/evaluation.html
  15. 8
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/templates/hello.html
  16. 38
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/templates/jinja-quiz.html
  17. 12
    0
      src/main/python/kr/co/swh/lecture/opensource/flask/templates/subjects.html
  18. 1
    1
      src/main/python/kr/co/swh/lecture/opensource/flask/url-variable1.py
  19. 19
    0
      src/main/python/kr/co/swh/lecture/opensource/logging/log.py

+ 1
- 0
.settings/org.eclipse.core.resources.prefs View File

@@ -1,5 +1,6 @@
1 1
 eclipse.preferences.version=1
2 2
 encoding//src/main/java=UTF-8
3
+encoding//src/main/python/kr/co/swh/lecture/opensource/logging/log.py=utf-8
3 4
 encoding//src/main/resources=UTF-8
4 5
 encoding//src/test/java=UTF-8
5 6
 encoding/<project>=UTF-8

+ 18
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/cookie.py View File

@@ -0,0 +1,18 @@
1
+from flask import Flask, request, make_response, render_template
2
+app = Flask(__name__)
3
+
4
+@app.route('/get')
5
+def get_cookie():
6
+    username = request.cookies.get('username')  # 클라이언트의 쿠키로부터 정보를 얻기
7
+    return render_template('cookie_hello.html', name=username)  # Html문자열을 Flask에서 암묵적으로 Response객체로 변환해서 브라우징
8
+    # resp = render_template('cookie_hello.html', name=username)
9
+    # return resp
10
+    
11
+@app.route('/add/<user>')
12
+def add_cookie(user):
13
+    resp = make_response(render_template('cookie_add.html', name=user)) # make_response함수는 개발자가 명시적으로 Response객체로 변환
14
+    resp.set_cookie('username', user)   # 쿠키로 정보를 저장 후 응답하면, 클라이언트 컴퓨터에 저장
15
+    return resp
16
+
17
+if __name__ == "__main__":
18
+    app.run(host="127.0.0.1", port="8080")

+ 14
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/error.py View File

@@ -0,0 +1,14 @@
1
+from flask import Flask
2
+app = Flask(__name__)
3
+
4
+@app.errorhandler(404)      # 에러코드
5
+def page_not_found(error):  # 에러를 위한 변수 전달
6
+    print(error)
7
+    return "<h1>없는 페이지예요.</h1>", 404    # 튜플형태(html페이지, 에러코드)
8
+
9
+@app.route("/")
10
+def hello():
11
+    return "<h1>SWHAcademy HelloWorld</h1>"
12
+
13
+if __name__ == "__main__":
14
+    app.run(host="127.0.0.1", port="8080")

+ 29
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/jinja-quiz.py View File

@@ -0,0 +1,29 @@
1
+from flask import Flask, jsonify, render_template
2
+import requests
3
+from bs4 import BeautifulSoup
4
+
5
+movieList = []
6
+app = Flask(__name__)
7
+app.config['JSON_AS_ASCII'] = False
8
+
9
+@app.route("/")
10
+def view():
11
+    return render_template("jinja-quiz.html", values=movieList)
12
+
13
+def naverMovie():
14
+    response = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.nhn')
15
+    html = response.text
16
+    soup = BeautifulSoup(html, 'html.parser')
17
+    ranking = 1
18
+
19
+    for tag in soup.select('div[class=tit3]'):
20
+        url = tag.get('href')
21
+        movie = dict()
22
+        movie["rank"] = str(ranking)
23
+        movie["title"] = tag.text.strip()
24
+        movieList.append(movie)
25
+        ranking = ranking + 1
26
+
27
+if __name__ == "__main__":
28
+    naverMovie()
29
+    app.run(host="127.0.0.1", port="8080")

+ 9
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/jinja_comment.py View File

@@ -0,0 +1,9 @@
1
+from flask import Flask, render_template
2
+app = Flask(__name__)
3
+
4
+@app.route('/<int:jumsu>')
5
+def evaluation(jumsu):
6
+   return render_template('comment.html', data=jumsu)    # 숫자로 전달하여 Html에서 값 비교
7
+
8
+if __name__ == "__main__":
9
+    app.run(host="127.0.0.1", port="8080")

+ 10
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/jinja_for.py View File

@@ -0,0 +1,10 @@
1
+from flask import Flask, render_template
2
+app = Flask(__name__)
3
+
4
+@app.route('/')
5
+def subject():
6
+   subjects = ['영어', '수학', '코딩']
7
+   return render_template('subjects.html', values=subjects)
8
+
9
+if __name__ == "__main__":
10
+    app.run(host="127.0.0.1", port="8080")

+ 9
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/jinja_if.py View File

@@ -0,0 +1,9 @@
1
+from flask import Flask, render_template
2
+app = Flask(__name__)
3
+
4
+@app.route('/<int:jumsu>')
5
+def evaluation(jumsu):
6
+   return render_template('evaluation.html', data=jumsu)    # 숫자로 전달하여 Html에서 값 비교
7
+
8
+if __name__ == "__main__":
9
+    app.run(host="127.0.0.1", port="8080")

+ 9
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/jinja_variables.py View File

@@ -0,0 +1,9 @@
1
+from flask import Flask, render_template
2
+app = Flask(__name__)
3
+
4
+@app.route('/hello/<user>')
5
+def hello(user):
6
+   return render_template('hello.html', name=user)
7
+
8
+if __name__ == "__main__":
9
+    app.run(host="127.0.0.1", port="8080")

+ 12
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/naver.py View File

@@ -0,0 +1,12 @@
1
+from flask import Flask 
2
+import requests
3
+
4
+app = Flask(__name__)   
5
+
6
+@app.route("/")
7
+def get_naver():
8
+    response = requests.get("http://www.naver.com")
9
+    return response.text 
10
+
11
+if __name__ == "__main__":
12
+    app.run(host="127.0.0.1", port="8080")

+ 2
- 2
src/main/python/kr/co/swh/lecture/opensource/flask/restful-quiz.py View File

@@ -1,4 +1,4 @@
1
-from flask import Flask, Response, jsonify
1
+from flask import Flask, jsonify
2 2
 import requests
3 3
 from bs4 import BeautifulSoup
4 4
 
@@ -7,7 +7,7 @@ app = Flask(__name__)
7 7
 app.config['JSON_AS_ASCII'] = False
8 8
 
9 9
 @app.route("/")
10
-def hello():
10
+def view():
11 11
     data = jsonify(movie)
12 12
     return data    # 일반 텍스트 형식으로 데이터를 전달
13 13
 

+ 20
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/templates/comment.html View File

@@ -0,0 +1,20 @@
1
+<html>
2
+	<head>
3
+  		<title>SWH Sample</title>
4
+  	</head>
5
+  	<body>
6
+	    {% if data >= 90 %}
7
+        <h3>수 </h3>
8
+        {#
9
+        {% elif data >= 80 %}
10
+        <h3>우</h3>
11
+        {% elif data >= 70 %}
12
+        <h3>미</h3>
13
+        {% elif data >= 60 %}
14
+        <h3>양</h3>
15
+        #}
16
+        {% else %}
17
+        <h3>가</h3>
18
+        {% endif %}
19
+  	</body>
20
+</html>

+ 8
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/templates/cookie_add.html View File

@@ -0,0 +1,8 @@
1
+<html>
2
+	<head>
3
+  		<title>SWH Sample</title>
4
+  	</head>
5
+  	<body>
6
+	    <h1>{{ name }}님 추가되었습니다.</h1>
7
+  	</body>
8
+</html>

src/main/python/kr/co/swh/lecture/opensource/flask/templates/jinja2.html → src/main/python/kr/co/swh/lecture/opensource/flask/templates/cookie_hello.html View File

@@ -1,8 +1,8 @@
1
-<html>
2
-	<head>
3
-  		<title>SWH Sample</title>
4
-  	</head>
5
-  	<body>
6
-	    <h1>{{ user }}님 환영합니다.</h1>
7
-  	</body>
1
+<html>
2
+	<head>
3
+  		<title>SWH Sample</title>
4
+  	</head>
5
+  	<body>
6
+	    <h1>{{ name }}님 환영합니다.</h1>
7
+  	</body>
8 8
 </html>

+ 18
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/templates/evaluation.html View File

@@ -0,0 +1,18 @@
1
+<html>
2
+	<head>
3
+  		<title>SWH Sample</title>
4
+  	</head>
5
+  	<body>
6
+	    {% if data >= 90 %}
7
+        <h3>수 </h3>
8
+        {% elif data >= 80 %}
9
+        <h3>우</h3>
10
+        {% elif data >= 70 %}
11
+        <h3>미</h3>
12
+        {% elif data >= 60 %}
13
+        <h3>양</h3>
14
+        {% else %}
15
+        <h3>가</h3>
16
+        {% endif %}
17
+  	</body>
18
+</html>

+ 8
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/templates/hello.html View File

@@ -0,0 +1,8 @@
1
+<html>
2
+	<head>
3
+  		<title>SWH Sample</title>
4
+  	</head>
5
+  	<body>
6
+	    <h1>{{ name }}님 환영합니다.</h1>
7
+  	</body>
8
+</html>

+ 38
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/templates/jinja-quiz.html View File

@@ -0,0 +1,38 @@
1
+<html>
2
+    <head>
3
+        <title>네이버 영화 랭킹</title>
4
+    </head>
5
+    <style>  
6
+		body { background: #fff; }
7
+		.bluetop {
8
+		  border-collapse: collapse;
9
+		  border-top: 3px solid #168;
10
+		}  
11
+		.bluetop th {
12
+		  color: #168;
13
+		  background: #f0f6f9;
14
+		}
15
+		.bluetop th, .bluetop td {
16
+		  padding: 10px;
17
+		  border: 1px solid #ddd;
18
+		}
19
+		.bluetop th:first-child, .bluetop td:first-child {
20
+		  border-left: 0;
21
+		}
22
+		.bluetop th:last-child, .bluetop td:last-child {
23
+		  border-right: 0;
24
+		}
25
+	</style>
26
+    <body>
27
+    	<table class="bluetop">
28
+    		<th>순위</th>
29
+   			<th>제목</th>
30
+   			{% for value in values %}
31
+   			<tr>
32
+   				<td>{{ value.rank }}</td>
33
+   				<td>{{ value.title }}</td>
34
+   			</tr>
35
+   			 {% endfor %}
36
+    	</table>
37
+    </body>
38
+</html>

+ 12
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/templates/subjects.html View File

@@ -0,0 +1,12 @@
1
+<html>
2
+    <head>
3
+        <title>Jinja 반복문</title>
4
+    </head>
5
+    <body>
6
+        <ul>
7
+            {% for value in values %}
8
+            <li>{{ value }}</li>
9
+            {% endfor %}
10
+        </ul>
11
+    </body>
12
+</html>

+ 1
- 1
src/main/python/kr/co/swh/lecture/opensource/flask/url-variable1.py View File

@@ -5,7 +5,7 @@ def hello():
5 5
     return "안녕하세요"
6 6
 
7 7
 @app.route("/<name>")   # 한글은 되지 않음
8
-def name_hello(name):
8
+def name_hello(name):   # 항상 전달되는 값은 문자열 형태
9 9
     return name + "야 안녕"
10 10
 
11 11
 if __name__ == "__main__":

+ 19
- 0
src/main/python/kr/co/swh/lecture/opensource/logging/log.py View File

@@ -0,0 +1,19 @@
1
+from flask import Flask
2
+import logging
3
+# 파일로 남기기 위해  filename='test.log' parameter로 추가한다.
4
+logging.basicConfig(filename='test.log', level=logging.INFO)
5
+
6
+app = Flask(__name__)
7
+
8
+@app.errorhandler(404)      # 에러코드
9
+def page_not_found(error):  # 에러를 위한 변수 전달
10
+    logging.debug(error)
11
+    return "<h1>없는 페이지예요.</h1>", 404    # 튜플형태(html페이지, 에러코드)
12
+
13
+@app.route("/")
14
+def hello():
15
+    logging.info("Hello")
16
+    return "<h1>SWHAcademy HelloWorld</h1>"
17
+
18
+if __name__ == "__main__":
19
+    app.run(host="127.0.0.1", port="8080")