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

skip Positron Proxy for fastapi and streamlit apps on PWB #5138

Merged
merged 21 commits into from
Oct 29, 2024

Conversation

sharon-wang
Copy link
Member

@sharon-wang sharon-wang commented Oct 23, 2024

QA Notes

I've been testing with the apps from https://github.com/posit-dev/qa-example-content/tree/main/workspaces/python_apps.

  • Positron on Workbench (Linux Ubuntu x86_64): Dash, Fastapi, Flask, Gradio, Streamlit all good ✅
    • Note that I sometimes have to refresh the Viewer for the Fastapi app before it works, as the app may be opened in the Viewer before the app is actually ready
  • Positron Server Web (Mac): Dash, Fastapi, Flask, Gradio, Streamlit all good ✅
  • Positron Desktop (Mac): Dash, Fastapi, Flask, Gradio, Streamlit all good ✅


// Preview the external URI.
positron.window.previewUrl(proxyInfo.externalUri);
positron.window.previewUrl(previewUri);
Copy link
Member Author

Choose a reason for hiding this comment

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

@seeM When running Fastapi in Positron on Workbench, sometimes the app url is previewed in the Viewer slightly too early, before the app server is ready. To get around this, a user can refresh the Viewer, but that is not ideal if it happens every time.

Do you have thoughts on how to get around this issue? Maybe we can ping the uri to check if it's responsive before displaying it in the Viewer?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe #4276 is describing what I'm seeing?

Copy link
Contributor

Choose a reason for hiding this comment

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

I assume that uvicorn would only print the URL to stdout once the server is ready to receive connections (I haven't confirmed that). Is the delay happening at the Positron Proxy layer? If so, maybe there's something we could expose from the Positron Proxy extension i.e. an awaitable or an event that we could listen to here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I looked into this a bit more and it does not seem to be in the Positron Proxy layer, as the issue still occurs even if we don't use the Positron Proxy.

For fastapi with uvicorn, we may want to wait for Application startup complete in the terminal output before we open the app at the url. We happen to be just a little quick to open the url/slow to startup the app on Workbench, such that the fastapi app is not quite ready yet.

Perhaps this would be a separate property in the terminal/debug config for some terminal text that indicates the app is ready, before we extract the app url and view it. I'll open a separate issue for this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Opened #5187

seeM
seeM previously approved these changes Oct 25, 2024
Copy link
Contributor

@seeM seeM left a comment

Choose a reason for hiding this comment

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

I haven't setup a workbench dev environment so I haven't tested the changes, but code changes look good. We should try to fix the fastapi preview if possible but don't think it has to block this PR.

extensions/positron-run-app/src/api.ts Outdated Show resolved Hide resolved
log.debug(`Viewing app at local uri: ${localUri} with external uri ${proxyInfo.externalUri.toString()}`);
// Determine the target origin based on whether the server is running in PWB.
const runningInPWB = process.env.RS_SERVER_URL ? true : false;
const targetOrigin = runningInPWB ? localBaseUri : localUri;
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting. I'm curious why the target origin differs here for workbench

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a bit of a hack as I couldn't get UVICORN_ROOT_PATH or --root-path to work for fastapi in Positron Server Web or Desktop. I haven't gotten to the bottom of why it's working on Workbench.

The result on Positron Server Web without this handling is that the openapi.json file is getting loaded from the root /openapi.json instead of from e.g. proxy/1234/openapi.json, so we get:

positron_web_fastapi_openapi_notfound

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm gonna move this PR to Draft while I try a few more things to see if we can avoid this custom handling.

Copy link
Member Author

@sharon-wang sharon-wang Oct 28, 2024

Choose a reason for hiding this comment

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

Positron Server Web still needs this workaround. I still don't understand why the root path setting doesn't work.

I've decided to not pass fastapi through the Positron Proxy for now. I'll probably do the same for Streamlit as well.


// Preview the external URI.
positron.window.previewUrl(proxyInfo.externalUri);
positron.window.previewUrl(previewUri);
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume that uvicorn would only print the URL to stdout once the server is ready to receive connections (I haven't confirmed that). Is the delay happening at the Positron Proxy layer? If so, maybe there's something we could expose from the Positron Proxy extension i.e. an awaitable or an event that we could listen to here?

@sharon-wang sharon-wang marked this pull request as draft October 28, 2024 17:05
@sharon-wang sharon-wang marked this pull request as ready for review October 28, 2024 21:30
@sharon-wang sharon-wang requested a review from seeM October 28, 2024 21:30
this allows us to run `vsce package` in the `extensions/positron-run-app` directory to generate a VSIX which can be loaded into Positron for faster extension dev iteration when running a built reh-web version of Positron.
Comment on lines +42 to +43
"@types/sinon-test": "^2.4.6",
"typescript": "^4.5.5"
Copy link
Member Author

Choose a reason for hiding this comment

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

this allows us to run vsce package in the extensions/positron-run-app directory to generate a VSIX, which can be loaded into Positron. This is helpful for faster extension dev iteration when running a pre-built reh-web version of Positron.

@sharon-wang sharon-wang changed the title Fix issues running fastapi apps on PWB Fix issues running fastapi and streamlit apps on PWB Oct 29, 2024
@sharon-wang sharon-wang changed the title Fix issues running fastapi and streamlit apps on PWB skip Positron Proxy for fastapi and streamlit apps on PWB Oct 29, 2024
@sharon-wang sharon-wang merged commit a896e6e into main Oct 29, 2024
3 checks passed
@sharon-wang sharon-wang deleted the fix-fastapi-on-workbench branch October 29, 2024 16:21
@github-actions github-actions bot locked and limited conversation to collaborators Oct 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants