Python SDK Tutorial -- News Application
Last updated
Last updated
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:
For more information on Neurelo’s Schema Language, refer to our Neurelo Schema Language (NSL) page.
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.
To learn how to install the Python SDKs, follow the steps outlined in the Python SDK page.
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:
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:
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,
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: