Browse Source

flask 진행 중

tobby48 5 years ago
parent
commit
b715b9d437

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

@@ -0,0 +1,13 @@
1
+from flask import Flask, url_for, redirect
2
+app = Flask(__name__)
3
+@app.route("/")
4
+def index():
5
+    return redirect('hello')    # 'hello', '/hello' 둘 다 가능
6
+    #return redirect(url_for('first'))
7
+
8
+@app.route("/hello")
9
+def first():
10
+    return "안녕하세요"
11
+
12
+if __name__ == "__main__":
13
+    app.run(host="127.0.0.1", port="8080")

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

@@ -0,0 +1,9 @@
1
+from flask import Flask, jsonify
2
+app = Flask(__name__)
3
+@app.route("/")
4
+def hello():
5
+    data = {'name': 'SWH', 'age': 3, 'subject': 'Coding'}   # 딕셔너리
6
+    return jsonify(data)    # JSON 형식으로 데이터를 전달
7
+
8
+if __name__ == "__main__":
9
+    app.run(host="127.0.0.1", port="8080")

+ 0
- 33
src/main/python/kr/co/swh/lecture/opensource/flask/templates/index.ejs View File

@@ -1,33 +0,0 @@
1
-<html>
2
-	<head>
3
-  		<title><%= title %></title>
4
-		<link rel="stylesheet" type="text/css" href="css/style.css">
5
-  	</head>
6
-  	<body>
7
-	    <h1><%= title %></h1>
8
-	    <table>
9
-			<thead>
10
-				<tr>
11
-					<th>랭킹</th>
12
-					<th>키워드</th>
13
-					<th>빈도수</th>
14
-				</tr>
15
-			</thead>
16
-			<tbody>
17
-				<% for(var i=0; i<values.length; i++){ %>
18
-				<tr>
19
-					<td>
20
-		                <%= values[i].rank %>
21
-	            	</td>
22
-	            	<td>
23
-		                <%= values[i].keyword %>
24
-	            	</td>
25
-	            	<td>
26
-		                <%= values[i].num %>
27
-	            	</td>
28
-	            </tr>
29
-	        	<% } %>
30
-		    </tbody>
31
-		</table>
32
-  	</body>
33
-</html>

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

@@ -6,10 +6,10 @@ def first():
6 6
 
7 7
 @app.route("/hello/<int:age>")
8 8
 def two(age):
9
-    "%d살이야." % age
9
+    return "%d살이야." % age
10 10
 
11 11
 if __name__ == "__main__":
12
-    with app.test_request_context():
12
+    with app.test_request_context():    # Flask 객체을 통해 테스팅
13 13
         print(url_for('first'))
14 14
         print(url_for('two', age=22))
15 15
     #app.run(host="127.0.0.1", port="8080")