https://ritza.co/articles/neon-flask/ [ ] [ ] Skip to content logo Ritza Articles Build a basic Flask app with Neon in 5 minutes ( ) [ ] Initializing search logo Ritza Articles * Ritza articles * Best Python Books For Beginners * Better Google Trends graphs using Python and seaborn * **Clubhouse summaries**: @shl and @jasonfried discuss deadlines * Creating Custom Graphs with Google Trends and pandas * DEV vs Medium vs Hashnode vs Hackernoon * GraphQL vs. REST vs. SQL vs. gRPC vs. OData vs. MongoDB * Heroku vs Netfliy vs Vercel vs GitHub Pages vs Firebase vs Vercel * Kubernetes vs Docker vs OpenShift vs ECS vs Jenkins vs Terraform * Learning Piano vs Learning Guitar vs Learning Keyboard vs Learning Violin vs Learning Cello * Matplotlib vs. seaborn vs. Plotly vs. MATLAB vs. ggplot2 vs. pandas * [ ] Build a basic Flask app with Neon in 5 minutes Build a basic Flask app with Neon in 5 minutes Table of contents + Signing up for Neon and getting a database + Building the Flask app + Install dependencies and run the app + Test your app + That's it * Note-generating metronome with Python * OpenGrok vs Sourcegraph vs GitHub vs FishEye vs Source Insight vs Elasticsearch * PyCharm vs Spyder vs Jupyter vs Visual Studio vs Anaconda vs IntelliJ * Raspberry Pi vs Arduino * Scikit-learn vs. TensorFlow vs. PyTorch vs. Keras * Tailwind CSS vs. Bootstrap vs. Material UI vs. Styled Components vs. Bulma vs. SASS * The MLOps Manifesto * The opinionated guide to setting up a Sourcegraph server for advanced code search * [ ] Gen articles Gen articles + Generated articles + bitcoin vs. ethereum vs. gold vs. dogecoin vs. cryptocurrency vs. dollar + flutter vs. react native vs. fibrillation vs. kotlin vs. swift vs. xamarin + freecodecamp vs. codecademy vs. bootcamp vs. udemy vs. teamtreehouse vs. coursera + laravel vs. django vs. symfony vs. wordpress vs. react vs. node + react vs. angular vs. vue vs. respond vs. javascript vs. node + strapi vs. contentful vs. sanity vs. wordpress vs. ghost vs. directus + webpack vs. babel vs. rollup vs. gulp vs. parcel vs. vite Table of contents * Signing up for Neon and getting a database * Building the Flask app * Install dependencies and run the app * Test your app * That's it Build a basic Flask app with Neon in 5 minutes Nearly all web applications require you to take input from a user, save it, and display it back to that user. With web frameworks like Flask, Express, Spring, Rails and many others, handling the frontend and backend is easy enough, but often it takes significant effort to set up a database to persist user data. And even if you invest the time, database hosting is often the most expensive part of any hosting bill. Neon is a new serverless Postgres database that: * Makes it ridiculously easy to get from account signup to a new Postgres database connection string (four clicks) * Gives you a significantly cheaper way to host your Postgres database (currently free). Let's spend 5 minutes to build a very simple Flask web application that can accept input from a user, store it, and display it back again. You should be able to easily adapt this tutorial to any other web framework you prefer. Signing up for Neon and getting a database * Sign up at Neon * Click "Create the first project". Create project * Confirm the auto-generated project name. Confirm project name * Close the config summary screen. Confirm project name * Click the "Copy" icon next to "Connection string" Get database connection string That's it. You don't need anything else from Neon to build a fully functional application. Building the Flask app We'll skip some best practices here to make the demonstration easier to follow, like hard coding our connection string in our application code and putting all our code in two files. Don't do this for anything you care about. * Create an app.py file in a project directory with the following code. Paste the connection string you copied in place of the postgresql://xxx. string in app.config and note that it should start with postgresql, not postgres. from flask import Flask, request, render_template from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config[ "SQLALCHEMY_DATABASE_URI" ] = "postgresql://xxxxxxxxx:xxxxxxxxxxxx@xxxxxxxxxxxxx.cloud.neon.tech:5432/main" db = SQLAlchemy(app) class Book(db.Model): book_id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String) db.init_app(app) db.create_all() @app.route("/", methods=["GET", "POST"]) def home(): if request.method == "POST": title = request.form.get("book") book = Book(title=title) db.session.add(book) db.session.commit() books = Book.query.all() return render_template("index.html", books=books) if __name__ == "__main__": app.run("0.0.0.0", debug=True) * Create a subdirectory in the same folder called templates and add the following frontend code to a file called index.html in the templates directory.