Giter VIP home page Giter VIP logo

fl-flask-maps's Introduction

Flask / PostGres app template

What is this template for?

As part of our course, we want to build a location-based web application. We know we will need a database to store some data, and that we want special support for geospatial data (latitude and longitude storage, searches based on distance, etc).

This teamplate is a guide on how to setup your application. You can clone / fork this repo to get the contents of some of the files, but otherwise, you will not build directly on top of this.

Also, through this guide:

  • The template includes sample code to show a Google Map and some markers in it
  • The template also includes a sample model with some prestored locations, just to test out the map functionality and make sure PostGis extension works too.

The idea is you use this to get a first working version of these basic functionalities, and then start changing things to build your own app.

Prerequisites

  • A GitHub account and a personal access token so you can commit stuff from the command line
  • Git installed in your machine so you can execute git commands
  • Python installed in your machine so you can execute Python commands and run Pyton scripts. Make sure you have version 3.6 or superior. You also need to use pip, can't remember if that needed to be installed separately.
  • A Google Maps API Key
  • A local installation of Postgres
brew install postgresql@14

Fork the project

Create a Python virtual environment:

Mac / Linux

python3 -m venv venv
. venv/bin/activate

Windows You need to figure out where is your Python executable first, that for me is (you will at least have to put your own username there instead of ):

virtualenv --python C:\Users\<username>\AppData\Local\Programs\Python\Python39\python.exe venv
.\venv\Scripts\activate

Installing Dependencies

pip install -f requirements.txt

install PostGres

Mac

install

brew install postgresql@14

start brew services start postgresql@14 stop brew services stop postgresql@14

to create a database

CREATE DATABASE <database_name>;

install postgis PostGIS is an open-source extension for the PostgreSQL relational database management system that adds support for geographic and spatial data. It enables PostgreSQL to store, query, and manipulate geospatial data, such as points, lines, polygons, and more. PostGIS provides a wide range of functions and features for geospatial analysis, including proximity searches, distance calculations, spatial joins, and map overlays. This extension is widely used in various applications, such as geographic information systems (GIS), mapping, and location-based services, where the storage and analysis of geographic data are essential. PostGIS is a valuable tool for managing and extracting insights from spatial information within a relational database environment.

brew install postgis

to create a postgis extension

CREATE EXTENSION postgis;

Running the app locally

  • DATABASE_URL: this is the connection string for the PostGres database. set postgress URI in terminal, do this every time you run the app export DATABASE_URL=postgresql://<username>:<password>@localhost:5432/<database_name>

  • GOOGLE_MAPS_API_KEY: You need to get this for yourself (via the Google app console). It will be sent to the Google Maps API to render the map on the initial page of the sample app. set google maps api in terminal do this every time you run the app export GOOGLE_MAPS_API_KEY=<api_key>

✴️ The environment variables you are setting now only 'exist' for as long as you keep the terminal / Powershell session open. When you close it and start again, the variables are gone, and you have to set them again! So you will do this step every time you start working on your app.

For the value for GOOGLE_MAPS_API_KEY you need to get the real api key value from your Google Apps console and use that value instead. Remember never to commit this API key to your repo because that is public and the key could get exploited / used by other people. Use of Google Maps API costs money after some limits, so be careful. If you publish your key by mistake you can invalidate it and create a new one.

If you want to verify the values of the env variables, use:

Mac / Linux

echo $DATABASE_URL
echo $GOOGLE_MAPS_API_KEY

Windows / Option 1

echo %DATABASE_URL%
echo %GOOGLE_MAPS_API_KEY%

Windows / Option 2

$env:DATABASE_URL
$env:GOOGLE_MAPS_API_KEY

Finally, to run the app:

**Mac / Linux / Windows **

flask run

You should see something like this on the output of the console:

 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

If you access http://127.0.0.1:5000/ or http://localhost:5000/ you should see:

sample app

Bonus: during development, you normally want to reload your application automatically whenever you make a change to it. You can do this by passing an environment variable, FLASK_ENV=development, to flask run:

FLASK_ENV=development flask run

Use Ctrl+C to quit / shut down the flask app.

Happy coding!

fl-flask-maps's People

Contributors

frannieziesemer avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.