Can I Use Python For Backend And React For Frontend?

Asked one year ago
Answer 1
Viewed 283
1

There are numerous potential approaches to utilizing Respond with a backend structure - - steps introduced beneath are showing one potential approach to interfacing those on PythonAnywhere to make rather a beginning stage for additional changes.

Prerequisites

It's expected that you have a functioning Node.js climate on your PythonAnywhere account - - in any case follow steps referenced on this help page.

Making a framework Respond frontend application

Accepting your web application (Django or Cup) is at ~/mysite registry:

cd ~/mysite
npx create-react-app frontend

Assuming that turns out great, enter the frontend registry and make a creation work of the Respond application:

cd frontend
npm run build

That will add a form index with a static subdirectory inside. See where index.html as well as css and js assets are found:

~/mysite/frontend $ tree -I node_modules
...
├── build
│   ├── asset-manifest.json
│   ├── favicon.ico
│   ├── index.html
│   ├── logo192.png
│   ├── logo512.png
│   ├── manifest.json
│   ├── robots.txt
│   └── static
│       ├── css
│       │   ├── main.073c9b0a.css
│       │   └── main.073c9b0a.css.map
│       ├── js
│       │   ├── 787.cda612ba.chunk.js
│       │   ├── 787.cda612ba.chunk.js.map
│       │   ├── main.b7ea7086.js
│       │   ├── main.b7ea7086.js.LICENSE.txt
│       │   └── main.b7ea7086.js.map
│       └── media
│           └── logo.6ce24c58023cc2f8fd88fe9d219db6c6.svg

Django

It's expected you made a Django web application in ~/mysite with our wizard Online application page.

settings.py
Open ~/mysite/mysite/settings.py.

Expecting that BASE_DIR is a pathlib.Path object (you might have to change the punctuation on the off chance that you use os.path all things considered), let Django know where to search for the essential index.html layout gave by Respond:

FRONTEND_DIR = BASE_DIR / "frontend"

TEMPLATES = [
    {
        ...
        "DIRS": [
            FRONTEND_DIR / "build",
        ],
        ...
    },
]

Then, guide Django toward the static assets given by the Respond fabricate:

STATIC_URL = "/static/"
STATICFILES_DIRS = [
    FRONTEND_DIR / "build/static",
]

urls.py

We should utilize a basic TemplateView serving index.html gave by the frontend application:

from django.views.generic import TemplateView

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", TemplateView.as_view(template_name="index.html")),
]

Flask

It's expected you made a Carafe web application in ~/mysite with our wizard Online application page.

flask_app.py

Open ~/mysite/flask_app.py and alter it so it contains:

from flask import Flask

app = Flask(__name__, static_folder="./frontend/build", static_url_path="/")

@app.route('/')
def index():
    return app.send_static_file('index.html')

You can likewise characterize a catch-all endpoint for all courses, which can be useful while utilizing respond switch:

from flask import Flask

app = Flask(__name__, static_folder='app')

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    return app.send_static_file("index.html")
Read Also : What are some of the challenges faced by the people living in Gaza?
Answered one year ago Evelyn HarperEvelyn Harper