pydantic / FastUI

Build better UIs faster.
https://fastui-demo.onrender.com
MIT License
8.24k stars 317 forks source link

fastui requires fastapi #85

Open jjgalvez opened 11 months ago

jjgalvez commented 11 months ago

Hi, first this looks like a cool project! I was trying your basic person list demo using litestar as the framework rather than fastapi. When I tried to the code I get an error saying that fastui.dev required fastapi to the installed. The exact error is:

from fastui import FastUI, AnyComponent, prebuilt_html, components as c
  File "/home/jc/.cache/pypoetry/virtualenvs/fastui-ls-4yKqtn9D-py3.11/lib/python3.11/site-packages/fastui/__init__.py", line 3, in <module>
    from .components import AnyComponent
  File "/home/jc/.cache/pypoetry/virtualenvs/fastui-ls-4yKqtn9D-py3.11/lib/python3.11/site-packages/fastui/components/__init__.py", line 17, in <module>
    from .forms import (
  File "/home/jc/.cache/pypoetry/virtualenvs/fastui-ls-4yKqtn9D-py3.11/lib/python3.11/site-packages/fastui/components/forms.py", line 9, in <module>
    from .. import forms
  File "/home/jc/.cache/pypoetry/virtualenvs/fastui-ls-4yKqtn9D-py3.11/lib/python3.11/site-packages/fastui/forms.py", line 19, in <module>
    raise ImportError('fastui.dev requires fastapi to be installed, install with `pip install fastui[fastapi]`') from e
ImportError: fastui.dev requires fastapi to be installed, install with `pip install fastui[fastapi]`

I did not install fastapi because I wanted to use litestar rather than fastapi.

samuelcolvin commented 11 months ago

There's a typo in the message, but the point is you need to use fastapi to use FastUI's form logic, you can use the components without installing FastAPI with any web framework.

BTW, I'd strongly recommend using a real web framework maintained by credible mature developers like Django, Flask, FastAPI or aiohttp.

jjgalvez commented 11 months ago

litestar (https://litestar.dev) looks real enough to me, and works well for the projects I'm using it for. That said IMHO if the form logic requires the use of fastAPI (fair enough I have nothing against fastAPI, it's a great framework) the documentation shouldn't say fastAPI isn't required

samuelcolvin commented 11 months ago

It's not required.

As I said above, FastAPI isn't required to instantiate and return components, the core of FastUI.

The form logic however is specific to the web framework where FastUI has some specific logic for FastAPI.

The readme could be more comprehensive, but it's not incorrect.

hasansezertasan commented 9 months ago

I simply tried to run this code block:

from fastui import prebuilt_html
from flask import Flask

app = Flask(__name__)

@app.get("/robots.txt")
async def robots_txt() -> str:
    return "User-agent: *\nAllow: /"

@app.get("/favicon.ico")
async def favicon_ico() -> str:
    return "page not found"

@app.get("/<path:path>")
async def html_landing():
    return prebuilt_html(title="FastUI Demo")

if __name__ == "__main__":
    app.run(debug=True)

I'm not using any components or so, only "prebuilt_html" but still...


Traceback (most recent call last):
  File "C:\Users\user\Desktop\flask-fastui\app.py", line 1, in <module>
    from src.app import app
  File "C:\Users\user\Desktop\flask-fastui\src\app.py", line 1, in <module>
    from fastui import prebuilt_html
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\flask-fastui-demo-lrUCYs1L-py3.11\Lib\site-packages\fastui\__init__.py", line 5, in <module>
    from .components import AnyComponent
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\flask-fastui-demo-lrUCYs1L-py3.11\Lib\site-packages\fastui\components\__init__.py", line 18, in <module>
    from .forms import (
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\flask-fastui-demo-lrUCYs1L-py3.11\Lib\site-packages\fastui\components\forms.py", line 8, in <module>
    from .. import events, forms
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\flask-fastui-demo-lrUCYs1L-py3.11\Lib\site-packages\fastui\forms.py", line 17, in <module>
    raise ImportError('fastui.dev requires fastapi to be installed, install with `pip install fastui[fastapi]`') from _e
ImportError: fastui.dev requires fastapi to be installed, install with `pip install fastui[fastapi]`

So I think @samuelcolvin is wrong because even if I only import prebuilt_html from FastUI, I still get this error that says "Install FastAPI"

samuelcolvin commented 9 months ago

Looks like we need to make some of the imports lazy to support that, probably best to add tests to ensure it stays that way.