Created
January 4, 2024 07:38
-
-
Save yvki/834f56150b16975d903351cdb6e80423 to your computer and use it in GitHub Desktop.
Hello World.py in 5 ways ππ»
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
# 1. Bottle framework | |
from bottle import route, run | |
@route('/') | |
def index(): | |
return '<h1>Hello World!</h1>' | |
run(host='localhost', port=8000, debug=True) | |
# 2. Django framework | |
# 2.1. Modify settings.py file | |
from django.conf import settings | |
settings.configure( | |
DEBUG=True, | |
SECRET_KEY='SECRET_KEY', | |
INSTALLED_APPS = [ | |
'django.contrib.admin', | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', | |
'django.contrib.messages', | |
'django.contrib.staticfiles', | |
], | |
ROOT_URLCONF=__name__, | |
MIDDLEWARE_CLASSES=( | |
'django.middleware.security.SecurityMiddleware', | |
'django.contrib.sessions.middleware.SessionMiddleware', | |
'django.middleware.common.CommonMiddleware', | |
'django.middleware.csrf.CsrfViewMiddleware', | |
'django.contrib.auth.middleware.AuthenticationMiddleware', | |
'django.contrib.messages.middleware.MessageMiddleware', | |
'django.middleware.clickjacking.XFrameOptionsMiddleware', | |
) | |
) | |
# 2.2. Modify views.py file | |
from django.http import HttpResponse | |
from django.conf.urls import url | |
def index(request): | |
return HttpResponse('<h1>Hello World!</h1>') | |
# 2.3. Modify urls.py file | |
urlpatterns = ( | |
url(r'^$', index), | |
) | |
# 2.4. Run main | |
if __name__ == '__main__': | |
from django.core.management import execute_from_command_line | |
import sys | |
execute_from_command_line(sys.argv) | |
# 3. Falcon framework | |
import falcon | |
from wsgiref.simple_server import make_server | |
app = falcon.App() | |
class HelloWorld: | |
def on_get(self, req, resp): | |
resp.status = falcon.HTTP_200 | |
resp.content_type = falcon.MEDIA_TEXT | |
resp.text = ( | |
'Hello World!' | |
) | |
index = HelloWorld() | |
app.add_route(r'/', index) | |
if __name__ == '__main__': | |
httpd = simple_server.make_server('127.0.0.1', 8000, app) | |
httpd.serve_forever() | |
# 4. Flask framework | |
from flask import Flask | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
return 'Hello World!' | |
if __name__ == '__main__': | |
app.run(debug=True) | |
# 5. Tornado framework | |
import tornado.web, tornado.httpserver, tornado.ioloop | |
class IndexHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.finish('<h1>Hello World!</h1>') | |
if __name__ == '__main__': | |
app = tornado.web.Application([(r'/', IndexHandler)]) | |
http_server = tornado.httpserver.HTTPServer(app) | |
http_server.listen(8000) | |
try: | |
tornado.ioloop.IOLoop.instance().start() | |
except KeyboardInterrupt: | |
print('Bye Bye World!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yvki's Opinions π on Listed Python π Web Frameworks π
How to Start β
pip install
1. Bottle πΎ
ππ» Lightweight nature due to lack of external dependencies offers simplicity for easier understanding and contribution towards quicker installation, development and deployment for small to medium-sized projects or prototypes
ππ» Limited functionality, unsuitability for large projects, and a potentially smaller codebase may pose challenges for developers working on complex applications that require scalability and a broader set of features
2. Django π
ππ» Offers a robust and feature-rich environment for developing large-scale web applications, "batteries-included" philosophy provides built-in components for common tasks, facilitating rapid development and maintaining best practices, ORM simplifies database interactions, and templating system aids in creating dynamic web pages
ππ» Extensive features and conventions may lead to a steeper learning curve for beginners, opinionated nature might limit flexibility in certain scenarios, powerful integrated administrative interface may introduce some overhead for projects not requiring such complexity
3. Falcon π¦
ππ» Lightweight and high-performance design makes it well-suited particularly for particularly developing APIs and RESTful services, simplicity allows for quick learning and straightforward implementation, and low overhead results in efficient handling of HTTP requests
ππ»Minimalism may be a drawback for those seeking a more feature-rich framework as it lacks built-in components for certain tasks that larger frameworks provide, developers may need to rely more on external libraries for additional functionalities beyond basic routing and request handling
4. Flask π«
ππ» Offers a balance between simplicity and flexibility well-suited for small to medium-sized projects, minimalistic design allows developers to choose and integrate components based on project needs which promotes a modular and customizable approach, lack of strict conventions provides freedom in structuring the application well-suited for beginners and adaptable to various development styles, lightweight nature and extensive community support further boosts its popularity as a choice framework for building web apps with specific requirements
ππ» Again, minimalism might be a limitation for larger and more complex projects that demand more built-in features, default also does not include an ORM or any authentication system thus requiring developers to integrate third-party solutions
5. Tornado πͺοΈ
ππ» Asynchronous nature and built-in support gives it an advantage in handling large numbers of simultaneous connections efficiently and is most suitable for applications with high concurrency requirements
ππ» Focus on asynchronous programming may lead to a steeper learning curve for developers unfamiliar with this paradigm, feature set is geared more towards handling network-oriented tasks which may not provide as many high-level abstractions for web development
Cool References
π The official home of Python programming language at https://www.python.org/
π The Python Package Index (PyPI) software repository at https://pypi.org/