Skip to content

Instantly share code, notes, and snippets.

@yvki
Created January 4, 2024 07:38
Show Gist options
  • Save yvki/834f56150b16975d903351cdb6e80423 to your computer and use it in GitHub Desktop.
Save yvki/834f56150b16975d903351cdb6e80423 to your computer and use it in GitHub Desktop.
Hello World.py in 5 ways πŸ–πŸ»
# 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!')
@yvki
Copy link
Author

yvki commented Jan 4, 2024

yvki's Opinions πŸ’­ on Listed Python 🐍 Web Frameworks 🌐

How to Start ❓

  1. Installations begin with the command pip install
  2. Import required modules when framework is installed successfully (as indicated by lighted-up name in VS Code editor)
  3. Specify the HTML appearance and configure connections/settings to relevant databases at that URL
  4. Run main code and open browser at the supported localhost port number (default is 8000)

1. Bottle 🍾

Bottle is a fast and simple micro-framework for small web applications. It offers request dispatching (Routes) with url parameter support, templates, a built-in HTTP Server and adapters for many third party WSGI/HTTP-server and template engines - all in a single file and with no dependencies other than the Python Standard Library.

πŸ‘πŸ» 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 πŸ”

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.

πŸ‘πŸ» 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 πŸ¦…

Falcon is a minimalist ASGI/WSGI framework for building mission-critical REST APIs and microservices, with a focus on reliability, correctness, and performance at scale. When it comes to building HTTP APIs, other frameworks weigh you down with tons of dependencies and unnecessary abstractions. Falcon cuts to the chase with a clean design that embraces HTTP and the REST architectural style. Falcon apps work with any WSGI or ASGI server, and run like a champ under CPython 3.5+ and PyPy 3.5+ (3.6+ required for ASGI).

πŸ‘πŸ» 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 πŸ«™

Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. Flask offers suggestions, but doesn’t enforce any dependencies or project layout. It is up to the developer to choose the tools and libraries they want to use. There are many extensions provided by the community that make adding new functionality easy.

πŸ‘πŸ» 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 πŸŒͺ️

Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed. By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user.

πŸ‘πŸ» 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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment