Starting with Flask
Introduction
This guide is going to help you start developing your Flask applications at SourceLair and make the most out of SourceLair's capabilities.
This guide assumes that you already have a SourceLair account
Setup
Flask is a simple but extensible python framework that helps you develop fast your python web apps. In SourceLair, in order to start a Flask app from scratch you need to:
- create a new project "From scratch"
- choose the "Custom" option
- choose the Python image
- choose the Python version that you want
- choose the "None" framework option
like in the following images:
SourceLair provides all Python projects with a dedicated, publicly accessible web server to run and view your web application. Starting a new Python project works out of the box for Flask thus you don't have to spend time on setup.
Flask server command
By default, your Python server runs the python server.py
command. In case you want to use any other custom command you can read how do it at the Customize your SourceLair server help page.
Cloning a Flask app
If you clone a Flask app at SourceLair, don't forget to keep your server file (e.g server.py
) in your root directory. In case you have a different structure please read our help page in order to customize your Procfile accordingly.
Installing dependencies
You can install dependencies to your Flask project in 2 ways:
- Terminal: Open your terminal, type and run
pip install -r requirements.txt
to install your dependencies from a requirements.txt file orpip install <package_name>
to install a specific package. - Command Palette: Open Command Palette, type
pip
and run thePip: Install pip dependencies
command that will install dependencies according to yourrequirements.txt
.
Preview your application
SourceLair provides you with a Public URL in order to help you test and showcase your work. There are 2 ways to see the public URL of your Flask project:
- Click on the eye icon in the sidebar
- Open the Command Palette and use the
Server: Open Public URL
command.
Deployment
The recommended way to deploy your Flask application to production is using Heroku. SourceLair's integration with the Heroku toolbelt makes things pretty simple as you can deploy your app without even leaving SourceLair.
Before you start setting up your deployment workflow, check out that you have:
Install and setup Gunicorn for production
While developing your application, you should run the Flask application server with the python server.py
command - you can find this inside the Procfile.dev
file.
When deploying your application for production load, you need an application server that can handle load better, like Gunicorn. That's why you need to create a Procfile
file that will give the appropriate instructions to Heroku about your server commands in production. So run in your terminal:
$ echo "web: gunicorn server:app --log-file -" > Procfile
In case that Gunicorn isn't installed in your project, run in your terminal:
$ pip install gunicorn
$ pip freeze | grep gunicorn >> requirements.txt
Set up Heroku
SourceLair terminal is integrated with Heroku toolbelt thus you are able to use all Heroku commands via SourceLair. Initially, you need to login to Heroku filling in your Heroku credentials
$ heroku login
Then, you need to create a new Heroku application that you can push to.
$ heroku create
Deploy
Don't forget to commit your last changes before deployment.
Everything should be set up, so you need deploy your app from SourceLair to Heroku. To do so use the Command Palette command Heroku: Deploy
.
Now every time you want to push something new to production, all you have to do is commit your changes and deploy to Heroku.
In case you use specific port in your Flask app (e.g. 5000), don't forget to bind the server of your Flask project to the
$PORT
environment variable, to make sure it works on Heroku.
Tips to code faster
SourceLair provides you with some great features in order to help you develop faster your Flask app being focused on the creative part of programming.
Go to definition
When searching for the definition of a class or method, you can just press Alt + . or Alt+ Click on it in order to go to its definition. Please note that when requesting the definition of an external library, goto definition will work as goto assignment instead.
Documentation
Read the documentation of a model or a class, focusing your cursor on it and using the Ctrl + I shortcut.
Command Palette
Command Palette is a free text search tool which helps you use most of SourceLair's features really fast. You can open it by pressing Ctrl/Cmd + Shift + P.
Quickopen
Quickopen is a fast and time saving way to navigate through your filesystem in order to find and open any file you want using your keyboard.
There are 2 ways to open the Quickopen prompt:
- using its shortcut Ctrl/Cmd + Shift + O
- finding
Quickopen: Open a file
command from Command Palette (Ctrl/Cmd + Shift + P)
Find in files
Find in files is a simple and easy way to search for something in your entire project at once, with quick file navigation from the results. There are 2 ways to trigger this feature:
- finding the
Project:Find in files
command from Command Palette - using its shortcut Ctrl/Cmd + Shift + F