Neurelo Build Docs
Neurelo Build Docs
  • Introduction
    • Core Concepts
    • Key Features
  • Getting Started
    • Sign-in/Sign-up
    • Dashboard
      • Collapsible Sidebar
      • Light/Dark Mode
      • Account Settings
      • Audit Events
      • User Management
        • Permissions (Member v/s Admin)
      • Org Settings
    • Starting your Neurelo Project
      • Quick Start Guide
      • Step 1 - Add a Data Source
      • Step 2 - Build Definitions
      • Step 3 - Create an Environment
      • Step 4 - Create an API Key
      • Step 5 - Start Runners
      • Try your Neurelo APIs
  • "How to" Videos
    • Product Overview
    • Neurelo APIs & SDKs
    • Project Setup
    • Definitions
    • Environments
    • Data Sources
    • Organization Management
    • Creating and Using Custom Queries
    • Using the Schema Builder to build Relationships
    • Mock Data Generation
  • Definitions
    • Neurelo Schema Editor
      • Schema Builder
      • JSON/YAML Editor
      • Schema Visualization: Entity-Relationship Diagram (ERD)
    • Custom APIs for Complex Queries
      • Write and Commit Custom Queries
      • AI-Assisted Query Generation
      • Deploying Custom API Endpoints
      • Using Variables in your Custom Query
    • Branches and Commits
    • API Docs
  • Environments
    • Creating a new Environment
    • API Playground
    • Observability
    • Migrations
    • API Keys
  • Data Sources
    • PostgreSQL
    • MySQL
    • MongoDB
  • Guides
    • Provisioning Cloud Databases for using with Neurelo
      • PostgreSQL
        • AWS RDS (PostgreSQL)
      • MySQL
        • AWS RDS (MySQL)
      • MongoDB Atlas
    • Mock Data Generation
    • Wipe Data Source
    • Remote Git Repository for Definitions
      • Connecting a Remote Git Repo
      • Creating Commits from Neurelo
      • Syncing Branches
    • Data Viewer
    • Environment/Data Source Tags
    • How to work with Embedded documents and References in MongoDB
    • How to download and use the Postman Collection for your Project
    • Building Python applications with Postgres and FastAPI
    • CI Integration using Neurelo CLI
    • Schema Migrations
    • Schema AI Assist
    • Auto-Introspection
    • Access Policies
    • User Auth
      • Google
      • GitHub
      • GitLab
    • MongoDB Atlas - Migrate GraphQL to Neurelo
    • MongoDB Atlas - Migrate REST Data APIs to Neurelo
  • MongoDB Atlas - Migrate REST Data APIs to Neurelo
  • MongoDB Atlas - Migrate GraphQL APIs to Neurelo
  • Neurelo Schema Language (NSL)
    • Example 1 - DVD Rentals
    • Example 2 - Simple "Posts" App
    • Example 3 - Bookstore
  • Neurelo API Reference (REST)
    • Examples of Neurelo Auto-Generated REST API endpoints
      • Example 1 - Simple “Posts” application
      • Example 2 - "DVD Rentals" application
      • Example 3 - "Bookstore” application
      • cURL API Examples
  • Neurelo API Reference (GraphQL)
  • SDKs
    • TypeScript / JavaScript SDK
    • Go SDK
    • Python SDK
      • Python SDK Tutorial -- News Application
        • News Application using Neurelo’s Python SDKs
  • CLI (Preview Version)
  • Self-Hosted Neurelo Gateways
  • Tutorials
    • Building a Real Time Chat Application with Neurelo and MongoDB using Python
    • Building A Financial Terminal with Neurelo and MongoDB in Rust
    • Building a Restaurant Management System with Neurelo and MongoDB using GraphQL in just a few minutes
    • Bringing Neurelo’s Data APIs to Life Instantly with MySQL
  • Project Examples
  • References
    • Supported Databases
    • Supported OS and Browsers
  • Support
Powered by GitBook
On this page
  • Overview
  • How to Setup and Download Neurelo’s SDKs
  • Building an application with Python SDKs
  • Environment Configuration
  • API Usage
  1. SDKs
  2. Python SDK

Python SDK Tutorial -- News Application

PreviousPython SDKNextNews Application using Neurelo’s Python SDKs

Last updated 1 year ago

Overview

How to Setup and Download Neurelo’s SDKs

In this tutorial, we'll dive into utilizing the Neurelo Python SDK to construct a news application from scratch.

To make things interesting, we are building a text-only news app, omitting photos or videos. This emphasizes clarity, conciseness, rapid loading, and accessibility, particularly for individuals with disabilities.

Our approach to building this news application involves envisioning a scenario where a cron job runs every few hours, triggering the execution of a Python script. This script is designed to scrape multiple RSS feeds for content, subsequently updating the application's database with fresh content while removing older news items. Additionally, when a user requests a specific news item, the application fetches the relevant content directly from the database.

At the heart of our news application lies Neurelo, playing a crucial role in facilitating interactions with our database through Neurelo’s Python SDKs.

Before delving into the SDKs and the application's construction, let's lay the groundwork by building a data source and strategizing a schema for our application. We will be using Postgres for this application.

To kick things off, let's create a New Project on Neurelo. Click on the New icon and input the project details.

Once the information is entered, click on Introspect to provide the connection details for inspecting your data source. You can bring your own database (as long as it is accessible to the internet!), or, for evaluation purposes, you can also create a Neurelo provisioned data source by selecting Empty Project on the How would you like to start? page and then navigating to the Data Sources tab.

With an empty data source in place, let's strategize on a schema. At its core, our application requires only a few fields, such as news title, category, summary, and hyperlink. Using this, we can create a schema as follows:

{
  "objects": {
    "News": {
      "properties": {
        "id": {
          "type": "integer",
          "default": {
            "function": "autoincrement"
          },
          "identifier": true
        },
        "category": {
          "type": "string"
        },
        "link": {
          "type": "string"
        },
        "published": {
          "type": "string"
        },
        "summary": {
          "type": "string"
        },
        "title": {
          "type": "string"
        }
      }
    }
  }
}

Let's commit this schema in the Definitions tab as outlined below:

Next, we will create an environment. Think of a Neurelo environment as a runtime setup where you can interact and run APIs. To create an environment, go to the Environments tab and click on the New button. Provide the necessary details, such as the desired commit for the environment, the environment region, and the associated data source.

Once the environment is created, applying a migration to our data source is a straightforward process. Head to the Migrations tab and validate the migrations that Neurelo has automatically generated for the schema and apply those migrations through Neurelo (if enabled), or download the migrations and apply them to the database using the workflows you may be using.

And there you have it! With the data source now synchronized with our schema, you can download Neurelo’s Python SDKs by going to the APIs tab and selecting the Python SDKs from the download menu.

Building an application with Python SDKs

Environment Configuration

Moving forward, let's create a News class and set up two methods to retrieve the environment configuration. This involves utilizing the Configuration and ApiClient instances as illustrated below:

from dotenv import load_dotenv
import os

from neurelo.configuration import Configuration
from neurelo.api_client import ApiClient

class News:
    def __init__(self) -> None:
        host, key = self.env()
        self.api_client = self.conf(host, key)

    def env(self):
        load_dotenv()
        return (os.getenv("NEURELO_API_HOST") or "", 
                os.getenv("NEURELO_API_KEY") or "")

    def conf(self, host, key):
        configuration = Configuration(host, api_key={"ApiKey": key})
        return ApiClient(configuration=configuration)

Later on, you'll notice that each individual schema model API can leverage the self.api_client. This API client plays a role in facilitating client-server communication.

To obtain the NEURELO_API_HOST, simply copy the host name mentioned in the Environments tab.

Additionally, you can generate a NEURELO_API_KEY by creating a new API Key from the API Keys tab.

Subsequently, you can export these values using a command similar to the following in your terminal:

$ export NEURELO_API_KEY="<YOUR-API-KEY>"
$ export NEURELO_API_HOST="<HOST-NAME>"

API Usage

Now, let's expand the functionality of the News class to store recent news articles and fetch them, all using our Python SDKs. For instance,

from dotenv import load_dotenv
import os

from neurelo.configuration import Configuration
from neurelo.api_client import ApiClient
from neurelo.api.news_api import NewsApi
from parse_news import Parse

class News:
    def __init__(self) -> None:
        host, key = self.env()
        self.api_client = self.conf(host, key)

    def env(self):
        load_dotenv()
        return (os.getenv("NEURELO_API_HOST") or "", 
                os.getenv("NEURELO_API_KEY") or "")

    def conf(self, host, key):
        configuration = Configuration(host, api_key={"ApiKey": key})
        return ApiClient(configuration=configuration)

    def fetch(self):
        news_api = NewsApi(self.api_client)
        news_item = news_api.find_news()
        return news_item

    def store(self):
        parse = Parse()
        news = parse.parse()
        articles = list(news.values())

        news_api = NewsApi(self.api_client)
        response = news_api.create_many_news(articles)
        return response

    def delete(self):
        news_api = NewsApi(self.api_client)
        deleted = news_api.delete_news()
        return deleted

All parameters for every operation come equipped with type hints, making it straightforward to discern the required model or determine if a raw value suffices.

And there you have it! Use the News class seamlessly with a web framework like Django or Flask to build your web application.

If you're curious about how the Parse class handles the fetching of news items from RSS feeds, take a look at the source code here:

For more information on Neurelo’s Schema Language, refer to our page.

To learn how to install the Python SDKs, follow the steps outlined in the page.

Neurelo Schema Language (NSL)
Python SDK
News Application using Neurelo’s Python SDKs