Python server with Flask

With Flask and SQLAlchemy I talked about in the last post, I was able to implement a python server. It is able to show a list of restaurants in the main page. Clicking on restaurant names show their menu, and each menu item can be edited or deleted. Users can also create new menu items. The database uses SQLAlchemy, and url resolving and HTTP requests handling use Flask, a microframework that makes implementations of web servers very easy.

The link to the project on GitHub is here.

With flask, resolving url addresses is simply app.route('format'), and under this line declare the method for this address. Here is an example:

This means whenever the address fits the format host/restaurants/int, the method restaurantMenu will be executed. restaurant_id is a variable extracted from the url and passed in the method as a parameter. Whatever this method returns shows up on the webpage. In this case, the method is returning the result of render_templaterender_template is a method in Flask that looks for the template specified in the first parameter in /templates and pass in necessary variables. menu.html looks like this:Screen Shot 2016-01-13 at 9.40.55 AM

items is the variable passed in this template. Here html execution arguments are used.

url_for is another tool used in this project. It is also included in Flask. The first parameter is the handler method of the product url, and other parameters are variables passed.

The next step is for methods to handle both GET and POST HTTP methods. Here’s how to do it:

methods need to be put in @app.route. Of course, request class also needs to be imported from Flask. Handling different kinds of methods is virtually the same as handling one. It’s only one if statement away.

Finally, for the server to repond to requests with JSON object, objects in the database need to have a function that returns all of their information.

In object class of the database setup python file:

And then use jsonify class provided by Flask to return json:
return jsonify(MenuItem = menuItem.serialize)
The json can then be used by any kind of application!

As a next step of this project, I plan to implement a server that works with iOS applications. I may need to write a framework for Swift to talk with my server. This means a lot of work, but it has great potential!

I’ll just forget about the fact that I have a midterm 30 minutes later and one more tomorrow.

20 thoughts on “Python server with Flask”

  1. OK I seriously need to resolve that http error when I upload pictures. I find that the files appear in wp-content but they do not show up in WordPress editor. It may be some owner or permission issue.

  2. Dear you are comprehensive in your thoughts. I have made something for guys to find their life partner. Please check my blog and share with people who need this guide.

  3. I spent four years trying EVERYTHING in Online Dating, and through a huge amount of trial and error, I produced a system that I will share for you. This book will take you, step by step, through everything you need to know to double, triple or even quadruple the number of women you meet online.

  4. Venus Viva™對所有皮膚類型都是安全的,並使用革命性的Nano Fractional Radio Frequency™(納米點陣射頻™)和Smart Scan™(智能掃描™)技術,通過選擇性真皮加熱,從而提供優異的治療效果。使用Nano Fractional RF™將能量透過表皮傳遞至真皮,從而產生熱量,並啟動膚膚的生理機制,重建膠原蛋白及刺激纖維母細胞,最終刺激導致組織重塑。功效:✔改善膚質✔肌膚緊緻✔減淡妊娠紋✔痤瘡及暗瘡疤痕✔減淡細紋及皺紋✔面部肌膚賦活再生 適合面部及頸部

  5. Echo Touch魔法保濕凝膠腳膜 韓國銷量已衝破一百萬對!!!!可以重覆用N次,無需過水既保濕凝膠襪子/手套Echo Touch魔法保濕凝膠手膜/腳膜,係韓國已經熱賣出一百萬對仲有韓國人氣節目Get it Beauty 都有介紹過,用完之後即時令手腳回復嫩滑,而且可以重複使用200-300次,99蚊用足成年,又抵用又環保。打開個包裝已經聞到好香,手套/襪子入面有一層gel,gel 既成份有維他命E、橄欖油,荷荷巴精油、綠

  6. 大熱刺繡 look! Marie Claire (HK) Edition Embroidery 元素從來無分季度,不但由秋冬紅到春夏,還能夠每年也不斷重複大熱!來到2018春夏,刺繡當然會出現於雪紡薄紗等質料上,再加入珠片元素即時令設計既復古又仙氣! 你也想入手一兩件刺繡

  7. Sculptra 含有由含有源於植物果酸的 PLLA 聚左乳酸(poly-L-lactic acid),它能促進天然膠原蛋白大量增生,令容顏重現年輕,一個療程的效果能持續逾2年。Sculptra 乃全球唯一獲美國食物及藥物管理局(美國FDA)認可的PLLA聚左乳酸美容品牌。Sculptra 塑然雅會被人體自然分解吸收,同時促進天然膠原蛋白再生1。療程效果可以漸進地給你更富清新朝氣的容頻,效果自然,更不會影響面部表情。SCULPTRA 塑然雅 Sculptra 由含有源於植物果酸的PLLA聚左乳酸(poly-L-lactic acid),它可以被新陳代謝成二氧化碳及水,最終自體內排出。PLLA聚左乳酸早已被廣泛應用於醫療用品超過30年,自2009年起 Sculptra 及全球唯一獲美國FDA認可的PLLA聚左乳酸美容醫學品牌。Sculptra ® 塑然雅能促進天然膠原蛋白大量增生1,改善皺紋,令容顏重現年輕。全球唯一獲美國FDA認可PLLA聚左乳酸。臨床研究證實於3個月間,肌膚的膠原蛋白密度 顯著增加67%2。療程效果能持續愈2年3。主要成份PLLA聚左乳酸源於植物果酸,醫學界廣泛使用超過30年。

  8. Have you ever thought about adding a little bit more than just your articles?I mean, what you say is important and everything.However just imagine if you added some great visuals or video clips to give your posts more,“pop”! Your content is excellent but with images and video clips,this website could undeniably be one of the best in its field.Good blog!

Leave a Reply

Your email address will not be published. Required fields are marked *