Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ASGI requirements #911

Merged
merged 5 commits into from
Jan 23, 2024
Merged

Update ASGI requirements #911

merged 5 commits into from
Jan 23, 2024

Conversation

dantownsend
Copy link
Member

Related to #907

  • Removing jinja2 as it's already a direct dependency of Piccolo
  • Removing requests as I don't think it's needed any more (I think this was for Starlette at some point)
  • Not installing piccolo_admin for esmerald

Piccolo has `jinja2` as a direct dependency, so this shouldn't be needed.
I think this was needed for Starlette at some point, but it has since transitioned to `httpx`, so I don't think it's needed any more.
We don't use it for `esmerald` so don't include it by default for all routers.
@dantownsend dantownsend added the enhancement New feature or request label Dec 12, 2023
@codecov-commenter
Copy link

codecov-commenter commented Dec 12, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (4fb169d) 87.91% compared to head (c0d6413) 78.71%.
Report is 6 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #911      +/-   ##
==========================================
- Coverage   87.91%   78.71%   -9.21%     
==========================================
  Files         107      108       +1     
  Lines        8145     8199      +54     
==========================================
- Hits         7161     6454     -707     
- Misses        984     1745     +761     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 13 to 23
ADMIN_DEPENDENCIES = ["piccolo_admin>=1.0.0"]
ROUTER_DEPENDENCIES = {
"fastapi": ["fastapi>=0.100.0"],
"starlette": ["starlette", *ADMIN_DEPENDENCIES],
"fastapi": ["fastapi>=0.100.0", *ADMIN_DEPENDENCIES],
"blacksheep": ["blacksheep", *ADMIN_DEPENDENCIES],
"litestar": ["litestar", *ADMIN_DEPENDENCIES],
# We don't currently use Piccolo Admin with esmerald, due to dependency
# conflicts with FastAPI. We'll revisit this at some point in the future
# when newer versions of FastAPI are released (DT - 12th December 2023).
"esmerald": ["esmerald"],
}
Copy link
Member Author

@dantownsend dantownsend Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to omit piccolo_admin from esmerald this way instead of having this in requirements.txt.jinja:

{% if "esmerald" not in router_dependencies %}
piccolo_admin
{% endif %}

Because at some point I'd like to version pin the routers a bit more. So it might be that we have esmerald>X.X.X in which case the if statement would fail.

piccolo[postgres]
piccolo_admin
requests
piccolo[postgres]>=1.0.0
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this will cause any problems ... i.e. it means that all of routers must support Pydantic v2. Will see when the CI runs.

@sinisaos
Copy link
Member

@dantownsend This should work because all router frameworks, used by Piccolo, support Pydantic v2, but I want to try each router template to be sure. I'll do it tonight because I can't right now, if you're okay with that?

@dantownsend
Copy link
Member Author

@sinisaos No worries - thanks!

Copy link
Member

@sinisaos sinisaos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes from this PR are good and don't affect how the Piccolo asgi template works, but I found two things that don't work. First is that Litestar Swagger docs don't work, but that's not on our side. Fix for that is made in Litestar main branch but not released yet (when they release new version everything will be fine - see issue). Second thing is for Esmerald. We have to remove "piccolo_admin.piccolo_app" only for Esmerald template from here

apps=["home.piccolo_app", "piccolo_admin.piccolo_app"]
because we don't use Piccolo Admin in Esmerald template at the moment. If we don't remove Piccolo Admin from APP_REGISTRY Unable to connect to database exception is raised and we can't run migrations for home app. We can use something like this.

{% if router == 'esmerald' %}
APP_REGISTRY = AppRegistry(
    apps=["home.piccolo_app"]
)
{% else %}
APP_REGISTRY = AppRegistry(
    apps=["home.piccolo_app", "piccolo_admin.piccolo_app"]
)
{% endif %}

It is also necessary to somehow notify the user that they cannot currently run session_auth and user migrations for Esmerald (dependency conflicts with FastAPI)

@dantownsend
Copy link
Member Author

@sinisaos Good points. Let's not worry about litestar, as you say - it'll be fixed when they release the next version.

With esmerald, omitting Piccolo Admin is definitely trickier than I imagined. I can add the if block to piccolo_conf.py.jinja like you suggest. And maybe just put something in the docs about support being beta at the moment.

@sinisaos
Copy link
Member

With esmerald, omitting Piccolo Admin is definitely trickier than I imagined. I can add the if block to piccolo_conf.py.jinja like you suggest. And maybe just put something in the docs about support being beta at the moment.

That would be great.

I found another small issue with Blacksheep and Pydantic v2. If we create a Pydantic model with BaseModel everything works, but when we create a Piccolo model with create_pydantic_model, the request body (and example value) is empty like this

empty_request_body

I think it's not a Piccolo problem, but a problem with the old version of SwaggerUI in BlackSheep. If I change [email protected] for CSS and JS to swagger-ui-dist@5 everything works fine like this

not_empty_request_body

I know you have been in contact with the author of BlackSheep for mounting the ASGI apps to BlackSheep (for Piccolo Admin). If you could raise issue in their repo, referring to this issue, that would be great.

@sinisaos
Copy link
Member

Litestar problem is solved with newest version (v2.4.4)

@sinisaos
Copy link
Member

BlackSheep issue will also be resolved with the next BlackSheep release (SwaggerUI has been upgraded in the BlackSheep main branch)

@dantownsend
Copy link
Member Author

@sinisaos Great, thanks a lot for checking that 👍

@sinisaos
Copy link
Member

@dantownsend FastAPI has been upgraded (Starlette >=0.29.0,<0.33.0) and we can now add Piccolo Admin to the Esmerald template. The integration test passed, but when I try to make a new Esmerald project in a fresh virtualenv, I had problems pip installing awesome-slugify (an Esmerald dependency) @tarsil, can you check it?

@tarsil
Copy link
Contributor

tarsil commented Dec 27, 2023

@dantownsend FastAPI has been upgraded (Starlette >=0.29.0,<0.33.0) and we can now add Piccolo Admin to the Esmerald template. The integration test passed, but when I try to make a new Esmerald project in a fresh virtualenv, I had problems pip installing awesome-slugify (an Esmerald dependency) @tarsil, can you check it?

Thank you @sinisaos I created actually some venvs in the past with all python versions and no issues were found. What error are you receiving?

@sinisaos
Copy link
Member

@tarsil I'm not at my machine right now, but when I can, I'll post the error log here. I tried Py3.9, Py3.10, Py3.11 and always got the same error (can't install awesome-slugify) when I try to pip install esmerald in fresh virualenv.

@tarsil
Copy link
Contributor

tarsil commented Dec 27, 2023

@tarsil I'm not at my machine right now, but when I can, I'll post the error log here. I tried Py3.9, Py3.10, Py3.11 and always got the same error (can't install awesome-slugify) when I try to pip install esmerald in fresh virualenv.

Also away for a couple of days and then I will be back at my machine but it is a first like this

@sinisaos
Copy link
Member

@tarsil I'm very sorry, but the problem was my machine (very strange, but somehow setuptools was not installed when I created a new virtualenv and that caused problems with installing Esmerald). After restarting the laptop, everything is fine.
@dantownsend I can confirm that you can add Piccolo Admin support to Esmerald template, everything works fine.

@tarsil
Copy link
Contributor

tarsil commented Dec 27, 2023

@tarsil I'm very sorry, but the problem was my machine (very strange, but somehow setuptools was not installed when I created a new virtualenv and that caused problems with installing Esmerald). After restarting the laptop, everything is fine. @dantownsend I can confirm that you can add Piccolo Admin support to Esmerald template, everything works fine.

Thank you @sinisaos for confirming. This was indeed odd but it could have happened

@dantownsend
Copy link
Member Author

@sinisaos Cool, thanks for checking. It'll be much easier to not exclude Piccolo Admin.

@sinisaos
Copy link
Member

@dantownsend Now that FastAPI is upgraded there is no need to exclude Piccolo Admin. Just need to add ADMIN_DEPENDENCIES to esmerald and add create_admin to Esmerald application class like this

# in _esmerald_app.py.jinja
from piccolo_admin.endpoints import create_admin
...
app = Esmerald(
    routes=[
        Gateway("/", handler=home),
        Gateway("/tasks", handler=TaskAPIView),
        Include(
            "/admin/",
            create_admin(
                tables=APP_CONFIG.table_classes,
                # Required when running under HTTPS:
                # allowed_hosts=['my_site.com']
            ),
        ),
    ],
    static_files_config=StaticFilesConfig(path="/static", directory=Path("static")),
    on_startup=[open_database_connection_pool],
    on_shutdown=[close_database_connection_pool],
)

and we can use Piccolo Admin in Esmerald template.

@dantownsend dantownsend merged commit 6ec06e7 into master Jan 23, 2024
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants