Generating and deploying Python code
Generating Python-based server code from your OpenAPI specification
Important: Ensure that you have created OpenAPI document beforehand.
The CodeGenerator tool creates Python FastAPI server code based on your OpenAPI specification. This code helps you quickly scaffold API endpoints defined in your OpenAPI file, so that you can focus on implementing business logic instead of writing boilerplate code. Follow the steps below to generate Python server code from your OpenAPI specification.
The CodeGeneration tool enables you to specify the type of server code you wish to generate.
Currently, API Assistant
supports two server types:
-
Simple Server: A minimalist server providing the basic setup to start building your service. For example:
Generate some Python FastAPI server code based on the following openapi @
-
Database CRUD Operations Server: An extension of the Simple Server that incorporates database logic to perform Create, Read, Update, and Delete (CRUD) operations, suitable for applications that require persistent data management. For example:
Build FastAPI backend code, including some database integration, using the OpenAPI file @
To generate a python code, complete the following steps:
- Log into API Assistant. For details, see Getting started.
- Enter the following prompt, and click
Send:
and select the required file, for example,Generate FastAPI server code from the openapi document @
purchaseorder_apispec.yaml
.Note: Entering @ after the prompt displays a list of OpenAPI documents available in your workspace. - Click Send. The CodeGenerator tool communicates with the API
Assistant sources and displays the proposed plan.
To view the plan details, expand the arrow icon
.
- Click Start to begin code generation. Once complete, you can view a brief summary of the next steps required to download the generated code as well as how to run and deploy it.
- Click the download icon to save the ZIP file containing the code.
- In VS Code, open the Terminal and run the following command to unzip the downloaded file
into a folder, for example,
app-code
.unzip PostgreSQL\ purchaseorder-python.zip
- Open the
README.md
file to view the instructions on how to get started with the downloaded code. TheREADME.md
provides detailed information on how to build, install, customize, and deploy the application. - Go to the Explorer pane, open the
database-props
file. - Replace the existing password with
9Iron-head
, and save the file. - Follow the steps in the Installation and Running the Server sections of the
README.md
file to build, install, and run the server.- Open a Terminal window and navigate to the extracted
folder:
cd app-code
- Create a virtual environment
python3 -m venv venv
- Activate the Virtual Environment.
- For Unix or macOS:
source ./venv/bin/activate
- For Windows:
.\venv\Scripts\activate
- For Unix or macOS:
- Install the required packages listed in
requirements.txt
pip install -r requirements.txt
- Run the server using the following
command
The server will be accessible at:PYTHONPATH=src uvicorn openapi_server.main:app --host 0.0.0.0 --port 8080
http://localhost:8080
- Open a Terminal window and navigate to the extracted
folder:
- To deploy the application, run the
deploy.py
script with the required input arguments as shown below.
Replace the placeholders with the following:python deploy.py <IBM Cloud API key> <IBM Cloud Resource Group name> <IBM Cloud Project name> <ICR Namespace name>
<IBM Cloud API key>
: The IAM API key for your IBM Cloud account.<IBM Cloud Resource Group name>
: The resource group to be used within the specified IBM Cloud account.<IBM Cloud Project name>
: A custom name for your IBM Code Engine project, application, and associated artifacts. This value will serve as a prefix for all IBM Code Engine artifacts created during deployment.<ICR Namespace name>
: The name of an existing IBM Container Registry (ICR) namespace within your IBM Cloud account. This namespace will serve as the destination where IBM Code Engine pushes the built container image for your application source code. Ensure this namespace exists in the global region of the ICR (for example,icr.io/<ICR Namespace name>
).
For example,
(replacepython deploy.py <ibm-cloud-apikey> default xxx-app-default testfest
uniqueapikey
with API key, andxxx-app-default
with your unique app name) - Once the script completes, a success message similar to the following will appear if the
deployment was successful:
App <IBM Cloud Project name>-app deployed successfully. App <IBM Cloud Project name>-app URL: https://<IBM Cloud Project name>-app.1o0xvbar1wkk.us-south.codeengine.appdomain.cloud
- Copy the app URL from the success message and paste it into a browser.
- Open the
openapi.yaml
file, copy the endpoint from the GET request, and append the endpoint to the app URL in the browser's address bar. - Run the request in the browser to view the application result.
- To modify the business logic, refer to the Customizing Logic section in
README.md
.