#python
#flask
Today I'm gonna show you about flask.
Requirements & Setup
Q/Of course, we need Python — 3.7 would be great
2/Pycharm community IDE because it’s awesome & free
3/Once you have the IDE, create a project. I named mine todo-flask. How creative!!
4/virtualenv — if you’re using Pycharm you don’t have to worry about this.
5/Installing flask using pip install flask
If you know the basics of Flask and wanna jump right in and build the app.
Let’s just check if our setup is fine by writing a “hello world” program. Create an app.py file under your project. You can name it anything other than calling your file flask.py since it will create a conflict with the actual flask package we installed.
Write down code below on app.py.
Run it from terminal.By default, flask runs a local server at port 5000.
Enjoy
#flask
Today I'm gonna show you about flask.
Requirements & Setup
Q/Of course, we need Python — 3.7 would be great
2/Pycharm community IDE because it’s awesome & free
3/Once you have the IDE, create a project. I named mine todo-flask. How creative!!
4/virtualenv — if you’re using Pycharm you don’t have to worry about this.
5/Installing flask using pip install flask
If you know the basics of Flask and wanna jump right in and build the app.
Let’s just check if our setup is fine by writing a “hello world” program. Create an app.py file under your project. You can name it anything other than calling your file flask.py since it will create a conflict with the actual flask package we installed.
todo-flask
|_ app.pyWrite down code below on app.py.
from flask import Flask
app = Flask(name)
@app.route("/")def hello():
return "Hello World!"
if name == 'main':
app.run(debug=True)Run it from terminal.By default, flask runs a local server at port 5000.
python app.pyEnjoy
Timepass with code
#python #flask Today I'm gonna show you about flask. Requirements & Setup Q/Of course, we need Python — 3.7 would be great 2/Pycharm community IDE because it’s awesome & free 3/Once you have the IDE, create a project. I named mine todo-flask. How creative!!…
#python
#Ternary
Today I'm showing you usages of else if
Let’s take the example of writing a small code to check if the person is a watchable or not, based on his age. Simply, initiating a variable age.
Using ternary conditional, this code is reduced to only a single line.
Whereas, the same logic can be implemented in the if-else as below.
Clearly, Ternary conditional makes the code 75% shorter and also improves the readability.
#Ternary
Today I'm showing you usages of else if
Let’s take the example of writing a small code to check if the person is a watchable or not, based on his age. Simply, initiating a variable age.
age = 18Using ternary conditional, this code is reduced to only a single line.
print("18+ site allowed for you") if age>18 else print("Not for you")Whereas, the same logic can be implemented in the if-else as below.
if age>18:
print("18+ site allowed for you")
else:
print("Not for you")Clearly, Ternary conditional makes the code 75% shorter and also improves the readability.
#python
#flask
#api
FastAPI is a modern web framework developed in Python using by Flask.FastAPI offers all the standard features of an API building tool but it is not limited to this. It comes with a lot of flexibility such as backward WSGI mounting support which, most of the users, are not aware of.
Today Include Flask, Django, Dash, or any other WSGI
Below are the example contents for the dashboard.py file:
#flask
#api
FastAPI is a modern web framework developed in Python using by Flask.FastAPI offers all the standard features of an API building tool but it is not limited to this. It comes with a lot of flexibility such as backward WSGI mounting support which, most of the users, are not aware of.
Today Include Flask, Django, Dash, or any other WSGI
Below are the example contents for the dashboard.py file:
from dash import html from dash import dcc import dash_bootstrap_components as dbc import dash dash_app = dash.Dash(name, requests_pathname_prefix='/dashboard/', title='mirror Demo') header = dbc.Row( dbc.Col( [ html.Div(style={"height": 30}), html.H1("Demo", className="text-center"), ] ), className="mb-4", ) dash_app.layout = dbc.Container( [ header, ], fluid=True, className="bg-light", )
For instance, this dashboard would be mounted on the “/dashboard” path. See the contents of the main.py file below:from fastapi import FastAPI from fastapi.middleware.wsgi import WSGIMiddleware from demo_dash import dash_app import uvicorn app = FastAPI() app.mount("/dashboard", WSGIMiddleware(dash_app.server)) @app.get('/') def index(): return "Hello" if name == "main": uvicorn.run("main:app", host='127.0.0.1', port=8000, reload=True)
If anyone visits the dashboard route, all those requests would be temporarily redirected to the dash server.
Timepass with code
#python #flask #api FastAPI is a modern web framework developed in Python using by Flask.FastAPI offers all the standard features of an API building tool but it is not limited to this. It comes with a lot of flexibility such as backward WSGI mounting support…
#python
#flask
#api
#Bonuse part
Mount different FastAPI apps for different routes
Below are the contents for the apiv1.py file (sub-application 1):
And here is the code for the apiv2.py file (sub-application 2):
Now, we can mount both of these sub-applications in the master application. See the code for the master.py file:
If you make requests to respective paths endpoints, the request will be handled by those sub-applications.
#flask
#api
#Bonuse part
Mount different FastAPI apps for different routes
Below are the contents for the apiv1.py file (sub-application 1):
from fastapi import FastAPI apiv1 = FastAPI() @apiv1.get('/returnName') def index(): return {"Name": "Mirror demo"}And here is the code for the apiv2.py file (sub-application 2):
from fastapi import FastAPI apiv2 = FastAPI() @apiv2.get('/returnLocation') def index(): return {"Location": "Bangladesh"}Now, we can mount both of these sub-applications in the master application. See the code for the master.py file:
from fastapi import FastAPI from apiv1 import apiv1 from apiv2 import apiv2 import uvicorn app = FastAPI() app.mount("/api/v1", apiv1) app.mount("/api/v2", apiv2) @app.get('/') def index(): return "Hello" if name == "main": uvicorn.run("master:app", host='127.0.0.1', port=8000, reload=True)If you make requests to respective paths endpoints, the request will be handled by those sub-applications.
🎉1
#python
#userbot
#plugin
⚡ 𝗕𝗶𝗻 𝗰𝗵𝗲𝗰𝗸 𝘂𝘀𝗲𝗿 𝗯𝗼𝘁 𝗽𝗹𝘂𝗴𝗶𝗻
♻️ 𝗗𝗲𝘃: @yourmirrorz
⚡ ƑìӀҽ Ӏìղҟ: https://t.me/Yourmirrorz_bot?start=VQxGBklqb9ZIhmm7
𝐣𝐮𝐬𝐭 𝐝𝐨 .install
𝗘𝗻𝗷𝗼𝘆 𝗺𝘂𝘀𝘁 𝗦𝗵𝗮𝗿𝗲 𝘄𝗶𝘁𝗵 𝗰𝗿𝗲𝗱𝗶𝘁.
#userbot
#plugin
𝐣𝐮𝐬𝐭 𝐝𝐨 .install
𝗘𝗻𝗷𝗼𝘆 𝗺𝘂𝘀𝘁 𝗦𝗵𝗮𝗿𝗲 𝘄𝗶𝘁𝗵 𝗰𝗿𝗲𝗱𝗶𝘁.
Please open Telegram to view this post
VIEW IN TELEGRAM
👍2🥰2
#python
#flask
#api
💖 𝗔𝗣𝗜𝘀 𝘂𝘀𝗶𝗻𝗴 𝗙𝗹𝗮𝘀𝗸
💻 Installation
❤️ Basics
🖥 Import
I began by importing Flask from flask. I imported Api from flask_restplus to define the application and Resource, which is received as a parameter in the numerous project-defined classes.
⚡ Define App
I defined a namespace here.
The URL for the namespace in the preceding example is http://127.0.0.1:5000/main, and its Swagger description is Main APIs.
⚡ Define APIs
In the preceding example, the APIs are accessible at http://127.0.0.1:5000/main. The class is named MainClass and contains two methods: get() and post(). Whenever I make a GET call, I receive a response with the status field reading Got new data, whereas a POST call returns the status field reading Posted new data.
✅ Run App
Now, everything is set up. The file is saved as basic.py.
Using pip, you can use the following command.
End.😎
#flask
#api
pipenv install flask
pipenv install flask-restpluspip install flask
pip install flask-restplusI began by importing Flask from flask. I imported Api from flask_restplus to define the application and Resource, which is received as a parameter in the numerous project-defined classes.
from flask import Flask from flask_restplus import Api, Resourceflask_app = Flask(name) app = Api(app = flask_app) name_space = app.namespace('main', description='Main APIs')I defined a namespace here.
The URL for the namespace in the preceding example is http://127.0.0.1:5000/main, and its Swagger description is Main APIs.
@name_space.route("/") class MainClass(Resource): def get(self): return { "status": "Got new data" } def post(self): return { "status": "Posted new data" }In the preceding example, the APIs are accessible at http://127.0.0.1:5000/main. The class is named MainClass and contains two methods: get() and post(). Whenever I make a GET call, I receive a response with the status field reading Got new data, whereas a POST call returns the status field reading Posted new data.
Now, everything is set up. The file is saved as basic.py.
pipenv shell
FLASK_APP=basic.py flask runUsing pip, you can use the following command.
FLASK_APP=basic.py flask runEnd.
Please open Telegram to view this post
VIEW IN TELEGRAM
👍1