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

Support local Python application development #4662

Merged
merged 53 commits into from
Sep 23, 2024
Merged

Conversation

seeM
Copy link
Contributor

@seeM seeM commented Sep 12, 2024

This PR adds support for local Python application development for the following frameworks:

  • Streamlit
  • Dash
  • Gradio
  • FastAPI
  • Flask

This is facilitated by a new extension: positron-run-app.

Addresses #4557, #4555, #3023, #3027, #3024.

Screen.Recording.2024-09-13.at.19.23.43.mov

QA notes

  1. Open an application developed using one of the supported frameworks.
  2. Run the relevant "Python: Run X App in Terminal" command. It should also be the default action if you click the run button in the editor menu.
  3. It should create a terminal named after the framework, and automatically show the app in the Viewer pane once its ready.

Here are example apps for all of the supported frameworks:

Streamlit

import streamlit as st
x = st.slider('x')
st.write(x, 'squared is', x * x)

Dash

from dash import Dash, html
app = Dash()
app.layout = [html.Div(children='Hello World')]
if __name__ == '__main__':
    app.run(debug=True)

Gradio

import gradio as gr
def image_classifier(inp):
    return {'cat': 0.3, 'dog': 0.7}
demo = gr.Interface(fn=image_classifier, inputs="image", outputs="label")
demo.launch()

FastAPI

from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
    return {"message": "Hello World"}

Flask

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

Next steps: Workbench integration

To support Workbench, we'll need to update runApplication to detect if we're in Workbench, and if so, find a free port and corresponding proxied URL (possibly related to #4274) and pass them both to getTerminalOptions.

@seeM seeM changed the title Draft: Generic "Run app" framework Support local Python application development Sep 13, 2024
@seeM seeM marked this pull request as ready for review September 13, 2024 17:44
Copy link
Contributor

@isabelizimm isabelizimm left a comment

Choose a reason for hiding this comment

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

I can go over this a bit deeper on Monday, but I think it seems like a reasonable start!

Potential other names I thought of are positron-app-runner and positron-app-viewer, although I don't think the current one is too bad 😄

extensions/positron-python/package.nls.json Outdated Show resolved Hide resolved
@seeM
Copy link
Contributor Author

seeM commented Sep 20, 2024

CI was failing due to cache issues so I've bumped the extensions cache key again in dd507f7 and let @midleman know.

@seeM
Copy link
Contributor Author

seeM commented Sep 20, 2024

Next CI error is because the test is running outside of a workspace. I can't figure out why, since that's not the case locally. Will try to debug next week or disable these tests in CI for now so we can merge once this is ready.

Copy link
Contributor

@isabelizimm isabelizimm left a comment

Choose a reason for hiding this comment

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

tried out with sample scripts and some community-built applications, and everything seems to run as expected!

side note: when I first built Positron, I ran into this error. After restarting it disappeared. noting in case this pops up again somewhere, but it seems unrelated to these changes

Command failed: . /Users/isabelzimmerman/code/positron-scratch/scratchvenv/bin/activate && echo 'e8b39361-0157-4923-80e1-22d70d46dee6' && python /Users/isabelzimmerman/code/positron/extensions/positron-python/python_files/printEnvVariables.py Debugger listening on ws://127.0.0.1:52539/19a0b899-0894-458d-8ae5-8a307458405d For help, see: https://nodejs.org/en/docs/inspector Debugger attached.

@seeM seeM merged commit 90b6806 into main Sep 23, 2024
23 checks passed
@seeM seeM deleted the positron-applications-api branch September 23, 2024 20:01
@github-actions github-actions bot locked and limited conversation to collaborators Sep 23, 2024
@sharon-wang
Copy link
Member

Testing on Workbench

(example apps from PR description)

Streamlit

Seems to be running just fine!

image

Dash

Message that popped up before running Dash app

image

After pip install python-dotenv and running Dash app

image

Gradio

image

  • app is loaded at correct path, but some of the files referenced in the app HTML are not loaded from the right path (the /s/<string>/p/<string> proxy path is missing in some of the hrefs)

FastAPI

image

Opening the proxied url directly

image

Changing the URL in the Viewer

image

  • this seems to imply that we're hitting the proxy/<port> path instead of the /s/<string>/p/<string> proxy path at some point. Direct access to /proxy/<port> is blocked on Workbench, so that's probably why it was failing to make the request.

Flask

Dialog that shows when I save the python file for the Flask app

image

After applying additional code snippet and running the app

image

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.

4 participants