GithubHelp home page GithubHelp logo

preprio / python-flask-quick-start Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 6 KB

The Flask Quick Start package covers the basics of connecting Prepr CMS with Flask. With the provided steps, you can query a basic page in less than 10 minutes.

python-flask-quick-start's Introduction

Flask Python Quick Start

The Flask Python quick start project launches a page app with content from Prepr.

Set up the project locally

Open the terminal and set up a quick virtual environment like the following:

python3 -m venv venv;
. venv/bin/activate;
pip install --upgrade pip

Now, you're ready to install dependencies for your project with pip:

pip install flask requests

You've now installed Flask, a microframework that you'll use for the simple web app to wrap data sent from the GraphQL API.

Set up the Flask app

Run the command below to set the Flask app location to the name of your application file, for example, test-page.py.

export FLASK_APP=test-page.py

Create your application file, test-page.py with the following code:

from flask import Flask

app = Flask(__name__)

@app.route("/get_page")
def get_page():
    return {"page title text": "data from the API"}

Type flask run in the terminal and open the browser on localhost:5000/get_page endpoint. A simple JSON with the "page title text" key is returned.

Query data from Prepr

To authenticate your app on Prepr CMS, you need to know the API URL and the GraphQL query you want to send to the request.

To get the endpoint of your Prepr environment, navigate to Settings -> Access Tokens and click the token you want to use. Copy the endpoint under API Url.

What's left is the GraphQL query that you want to pass to the request. Click Open in API Explorer to open the explorer in which you can experiment with GraphQL queries. Write this query on the playground:

query Page {
  Page(slug: "home") {
    title   
  }
}

Now, you're ready to authenticate your app. Open the test-page.py, and your project is now like the following:

import requests
import json
import os
from flask import Flask

query_page = """query Page {
  Page(slug: "home") {
    title   
  }
}
"""

url = "https://graphql.prepr.io/your_token"  # Paste your earlier copied API URL
app = Flask(__name__)

@app.route("/get_page")
def get_page():
    payload = {"query": query_page}
    result = requests.post(url, json=payload)
    json_data = result.json()
    return {"page title text": json_data["data"]["Page"]["title"]}
  • The result object contains the response of the API request that you sent.
  • The json_data is a JSON conversion from the result Python object
  • Finally, the response coming from the query:
{
  "data": {
    "Page": {
      "title": "Home page"
    }
  }
}

Run the Flask app with flask run and open the localhost on the get_page endpoint. You'll see a response with the following:

{
  "page title text": "Home page"
}

python-flask-quick-start's People

Contributors

tim-hanssen avatar

Watchers

 avatar  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.