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 Flask docs and examples #223

Merged
merged 7 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/apps/django.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Now, open [http://0.0.0.0:5000/](http://0.0.0.0:5000/) to see your app.

Once you have all your files, create a zip file.

To deploy a Flask app from the deployment menu, follow these instructions:
To deploy a Django app from the deployment menu, follow these instructions:

![](../static/docker.png)

Expand Down
8 changes: 4 additions & 4 deletions doc/apps/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ To deploy a Docker-based web application in Ploomber Cloud you need:

## `Dockerfile`

You need to provide a `Dockerfile`, you can use this [flask-based template](https://github.com/ploomber/doc/blob/main/examples/flask/basic-app/Dockerfile) to get started.
You need to provide a `Dockerfile`, you can use this [FastAPI-based template](https://github.com/ploomber/doc/blob/main/examples/fastapi/basic-app/Dockerfile) to get started.

For a successful deployment, you app must run in port 80.

Here's an example using flask:
Here's an example using FastAPI:

```Dockerfile
FROM python:3.11

COPY app.py app.py
RUN pip install flask gunicorn
RUN pip install fastapi uvicorn --no-cache-dir

ENTRYPOINT ["gunicorn", "app:app", "run", "--bind", "0.0.0.0:80"]
ENTRYPOINT ["uvicorn", "app:app", "--host=0.0.0.0", "--port=80"]
```

Once you have all your files, `.zip` them. For example, a simple app will contain two files:
Expand Down
79 changes: 55 additions & 24 deletions doc/apps/flask.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,79 @@ myst:
To deploy a Flask application in Ploomber Cloud you need:

- A [Ploomber Cloud](https://platform.ploomber.io/register?utm_source=flask&utm_medium=documentation) account
- A `Dockerfile`
- Your code

## `Dockerfile`

You need to provide a `Dockerfile`, you can use this [template](https://github.com/ploomber/doc/blob/main/examples/flask/basic-app/Dockerfile) to get started. The template contains the minimal steps needed for a deployment but you need to modify so it installs any required dependencies and copies your code into the Docker image.

```Dockerfile
FROM python:3.11

# assumes app.py contains your flask app
COPY app.py app.py
# install flask and gunicorn
RUN pip install flask gunicorn

# this configuration is needed for your app to work, do not change it
ENTRYPOINT ["gunicorn", "app:app", "run", "--bind", "0.0.0.0:80"]
```
- Your application file (`app.py`)
- A dependencies file (`requirements.txt`)

## Testing locally

To test your app, you can use `docker` locally:
To test your app, you can use `gunicorn` locally:

```sh
# build the docker image
docker build . -t flask
# Install requirements
pip install -r requirements.txt

# run it
docker run -p 5000:80 flask
# Start the flask application
gunicorn app:server run --bind 0.0.0.0:5000
```

Now, open [http://0.0.0.0:5000/](http://0.0.0.0:5000/) to see your app.


## Deploy

`````{tab-set}

````{tab-item} Web
__Deploy from the menu__

Once you have all your files, create a zip file.

To deploy a Flask app from the deployment menu, follow these instructions:

![](../static/docker.png)
![](../static/flask.png)
````

````{tab-item} Command-line
__Try an example__

To download and deploy an example Flask application start by installing Ploomber Cloud and setting your API key:

```sh
pip install ploomber-cloud
ploomber-cloud key YOUR-KEY
```

```{tip}
If you don't have an API key yet, follow the [instructions here.](../quickstart/apikey.md)
```

Now, download an example. It will prompt you for a location to download the app. To download in the current directory, just press enter.

```sh
ploomber-cloud examples flask/basic-app
```

```{note}
A full list of Flask example apps is available [here.](https://github.com/ploomber/doc/tree/main/examples/flask)
```

You should see a confirmation with instructions on deploying your app. Now, navigate to your application:

```sh
cd location-you-entered/basic-app
```

__Deploy from the CLI__

Initialize and deploy your app with:

```sh
ploomber-cloud init
ploomber-cloud deploy --watch
```

````
`````

```{tip}
To ensure your app doesn't break on re-deployments, pin your [dependencies.](pin-dependencies)
Expand Down
Binary file added doc/static/flask.png
Copy link
Contributor

@neelasha23 neelasha23 May 14, 2024

Choose a reason for hiding this comment

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

The Scheduled Functions can be removed. Refer to this comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in this commit.

Not sure why the change isn't updating in the readthedocs build but it renders correctly locally and you should see the file has been changed

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 0 additions & 6 deletions examples/flask/basic-app/Dockerfile

This file was deleted.

Binary file modified examples/flask/basic-app/app.zip
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/flask/basic-app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask
gunicorn
7 changes: 0 additions & 7 deletions examples/flask/login/Dockerfile

This file was deleted.

5 changes: 3 additions & 2 deletions examples/flask/login/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export FLASK_SECRET_KEY=MYFLASKSECRETKEY

## Deployment

1. [Create an account in Ploomber Cloud](https://platform.ploomber.io/register)
2. [Follow this](https://docs.cloud.ploomber.io/en/latest/apps/flask.html)
1. Create the database models by running `python models.py`
2. [Create an account in Ploomber Cloud](https://platform.ploomber.io/register)
3. [Follow this](https://docs.cloud.ploomber.io/en/latest/apps/flask.html)

Remember to add the `FLASK_SECRET_KEY` environment variable.

Expand Down
9 changes: 0 additions & 9 deletions examples/flask/machine-learning/Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion examples/flask/machine-learning/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Execute the `walmart_sales.ipynb` notebook.
ploomber-engine walmart_sales.ipynb output.ipynb
```

3. Create a zip file from `Dockerfile`, `app.py`, `walmart_sales_rf.joblib`, `static/` and `templates/`.
3. Create a zip file from `app.py`, `walmart_sales_rf.joblib`, `static/` and `templates/`.

4. Login to your [Ploomber Cloud](https://ploomber.io/) account.

Expand Down
1 change: 1 addition & 0 deletions examples/flask/machine-learning/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ joblib
numpy
pandas
scikit-learn
gunicorn