Skip to content

Instantly share code, notes, and snippets.

@EmbraceLife
Last active August 4, 2024 15:48
Show Gist options
  • Save EmbraceLife/8cbf27ad82dfa5db137efee33e258809 to your computer and use it in GitHub Desktop.
Save EmbraceLife/8cbf27ad82dfa5db137efee33e258809 to your computer and use it in GitHub Desktop.

Web Application: what does it take

What it is like before fasthtml

Front-end Development

Programming Languages:

  • HTML: Structures web pages.
  • CSS: Styles web pages.
  • JavaScript: Adds interactivity.

Frameworks/Libraries:

  • React: Builds user interfaces.
  • Angular: Creates single-page apps.
  • Vue.js: Flexible UI framework.
  • Bootstrap: Responsive design framework.

Back-end Development

HTTP: Protocol for web communication.

Languages:

  • JavaScript (Node.js): Server-side JavaScript.
  • Python: Used with Django, Flask.
  • Java: Used with Spring.
  • Ruby: Used with Rails.
  • PHP: Server-side scripting.

Frameworks/Libraries:

  • Express.js: Node.js web framework.

  • Django: Python web framework.

  • Flask: Lightweight Python framework.

  • Spring: Java enterprise framework.

  • Ruby on Rails: Ruby web framework.

  • Uvicorn: Fast ASGI server for Python.

  • Starlette: Lightweight ASGI framework for async web services.

  • FastAPI: Fast Python web framework for APIs, built on Starlette and Uvicorn.

fastHTML: easier and better

General reasons

  1. Python-Centric: FastHTML lets you build web apps using Python, easing the learning curve for those familiar with Python.

  2. Simplicity: FastHTML is straightforward and easier to learn than React, Angular, or Vue, making it ideal for beginners.

  3. Full-Stack in One Language: FastHTML allows you to handle both front-end and back-end in Python, reducing context-switching.

  4. Performance: FastHTML generates HTML on the server for fast page loads and integrates with HTMX for dynamic updates.

  5. Gradual Enhancement: Start with Python and add JavaScript as needed, allowing for a smooth learning progression.

  6. Community: FastHTML and Python communities are supportive and beginner-friendly.


Technical Reasons

Better mapping html elms with its host language

like React using javascript to integrate html, css and javascript together, fasthtml use python to integrate the three and make it a lot more easier, see Jeremy's tweet on how fasthtml generate html tags with more easy and joy.

I've had quite a few people ask if we plan to add syntax like jsx to FastHTML. We don't. That's because Python's function syntax is already a perfect 1:1 match to html element trees. JS isn't, so they needed something new. Here's an example I just showed on the HTMX discord:

Actually we don't even need the f-string. H1('Hello ', name) would work too...

![[Pasted image 20240804193633.png]]

Deeper integration with HTMX

HTMX Capabilities

  • Interactivity: Enhances HTML by enabling dynamic interactions through attributes, allowing server communication without full page refreshes.
  • Simple Integration: Adds interactivity directly to HTML tags, minimizing the need for JavaScript.

Traditional Frameworks (React, Angular, Vue) don't use htmx

  • React: Uses a virtual DOM for efficient updates.

  • Angular: Utilizes two-way data binding.

  • Vue: Employs a virtual DOM and reactive data binding.

  • Full SPA Support: Manage interactivity, state management, and routing, using a virtual DOM for efficient updates.

  • Single-Page Application (SPA): A web application that loads a single HTML page and updates dynamically to provide a seamless user experience without full page reloads.

How HTMX Mimics SPAs

  • Dynamic Behaviors: Uses attributes to fetch data and update parts of the page without full reloads, offering SPA-like interactions.
  • Less Complexity: Avoids managing virtual DOM or complex state management, working directly with existing HTML.

When to use htmx

  • Ideal for simpler web applications that don't need the full capabilities of frameworks like React or Vue.
  • Used to add interactivity to server-rendered pages without heavy JavaScript frameworks.
  • Often paired with lightweight backend frameworks like Flask or Django for quick development and reduced complexity.
  • Best for projects needing simplicity, minimal overhead, and straightforward interactivity.

fastHTML with htmx

  • Makes effective use of HTMX to enhance server-side rendered applications, allowing interactive web experiences with minimal JavaScript.

How fastHTML use htmx differently from Django and Flask

  • Django and Flask use HTMX to enhance interactivity within their existing frameworks,
  • FastHTML integrates HTMX more deeply, offering a more seamless and efficient approach to building dynamic, interactive web applications.
  • This makes FastHTML particularly well-suited for real-time applications and projects that benefit from frequent updates and server-client interactions.

Simpler and more capable with Uvicorn and Starlette

Django

  • WSGI-Based: Designed for synchronous web applications.
  • Channels: Extends Django for asynchronous operations using an ASGI interface, mainly for WebSockets and real-time features.

Flask

  • WSGI-Based: Synchronous by default.
  • ASGI Transition: Can be adapted for asynchronous needs using tools like Quart or by migrating specific features to FastAPI.

Why not just use Django or Flask?

Advantages of Using Uvicorn and Starlette

  1. Native Asynchronous Support:

    • Uvicorn: A high-performance ASGI server leveraging Python's asyncio, ideal for real-time applications requiring high concurrency, such as chat and live updates.
  2. Lightweight and Minimalistic:

    • Starlette: A lightweight ASGI framework offering essential features like WebSockets and background tasks, without the complexity of a full framework like Django.
  3. Performance and Scalability:

    • Offers superior performance by efficiently handling asynchronous I/O, suitable for applications with high concurrency and minimal latency, ideal for microservices architectures.
  4. Microservices and Serverless Architectures:

    • Starlette is well-suited for microservices and serverless applications, providing small, isolated functionality units, unlike the more monolithic approach of Django.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment