Coder Social home page Coder Social logo

fastapi-usecase's Introduction

FastAPI Use Case

A collection of FastAPI use cases. Work in progress.

How to use

  1. Clone this repo
  2. Install deps with poetry install
  3. Run poetry run uvicorn main:app --reload

Use Case

  1. File-based Routing With Jinja Templating

Normally, we will create one function for one path, hardcode the template path and give the context (data). With file-based routing, we only need to define a single path that catches all, then create our template and function in the directory with the same name with path.

See main.py and search for catch_all functions. It will search for index.html in the same path as request and if get_context function exists in __init__.py then it will be pass the context to the template. You can see the hierarchy here in src/file-based-templates.

For Example:

example.com ==> /index.html
example.com/about ==> /about/index.html
example.com/nested/path/should/works ==> /nested/path/should/works/index.html

It will also looking for __init__.py for context

Note 1: It's inspired by Next.js file-based routing (okay, PHP do that in the very beginning).

Note 2: I also try to use HTMX with hx-boost. So if I click a navigation link, it will not reload the whole page but only replace the element that changed. Just like modern JS framework

  1. Hot reload the HTML in browser

TBA. Great candidate: https://github.com/florimondmanca/arel. The example of usage https://gist.github.com/vrslev/6d0602bfa939a01844f645c608afb85a

Using uvicorn main:app --reload --reload-include *.html will reload the uvicorn but not the browser


Have any other ideas for use case? Request in the issues section.

fastapi-usecase's People

Contributors

tegarimansyah avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.