Created
August 5, 2020 07:39
-
-
Save wfng92/42269866a47298e7409b04f4b200bfac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import declaration | |
from flask import Flask, request, jsonify, render_template, send_from_directory | |
import random | |
# initialization | |
app = Flask(__name__) | |
# hello world, GET method, return string | |
@app.route('/') | |
def hello(): | |
return "Hello World!" | |
# random number, GET method, return string | |
@app.route('/random-number') | |
def random_number(): | |
return str(random.randrange(100)) | |
# check isAlpha, GET method, query parameter, return JSON | |
@app.route('/alpha', methods=['GET']) | |
def alpha(): | |
text = request.args.get('text', '') | |
result = {'text': text, 'is_alpha' : text.isalpha()} | |
return jsonify(result) | |
# create new user, POST method, form fields, return JSON | |
@app.route('/create-user', methods=['POST']) | |
def create_user(): | |
id = request.form.get('id', '0001') | |
name = request.form.get('name', 'Anonymous') | |
# code for authentication, validation, update database | |
data = {'id': id, 'name': name} | |
result = {'status_code': '0', 'status_message' : 'Success', 'data': data} | |
return jsonify(result) | |
# update language, PUT method, JSON input, return string | |
@app.route('/update-language', methods=['POST', 'PUT', 'GET', 'DELETE']) | |
def update_language(): | |
language = 'english' | |
if request.method == 'PUT': | |
json_data = request.get_json() | |
language = json_data['language'] | |
return "Successfully updated language to %s" % (language) | |
# serve webpage, GET method, return HTML | |
@app.route('/get-webpage', methods=['GET']) | |
def get_webpage(): | |
return render_template('index.html', message="Contact Us") | |
# file response, GET method, return file as attachment | |
@app.route('/get-language-file/<string:language>', methods=['GET']) | |
def get_language_file(language): | |
return send_from_directory('./static/language', language + '.json', as_attachment=True) | |
# main | |
if __name__ == '__main__': | |
app.run('0.0.0.0',port=8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment