Chrome Extension Requesting django server
Question:
I am creating a chrome extension that requires a back end server for processing. I would like it to send data to the server, the server will process this data and send some other data back to the user. Preferably this back end server should be in python as there are libraries I would like to use. Any idea how to go about this?
I was thinking to use a django server with rest API but I’m not sure what would be best. If anyone could provide a tutorial link or briefly explain the code behind this idea it would be brilliant.
Answers:
I have produced a solution. You are able to send requests to a django server like any other server using JSON files. Here is my implementation:
mainproject/urls.py
urlpatterns = [
path("",include("sentimentProcessing.urls"))
]
sentimentProcessing/urls.py
urlpatterns = [
path('process_sentiment/', process_sentiment),
]
sentimentProcessing/views.py
# Create your views here.
@csrf_exempt
@require_POST
def process_sentiment(request):
data = json.loads(request.body)
# Process sentiment
sentiment = sentiment_prediction(data) # For example, here you could process data in your own way, using your own function
# Serialize the processed data to JSON
response = {"sentiment":f"{sentiment}"}
# Return a JSON response with the processed data
return JsonResponse(response)
There are also some installed apps for cors in settings.py.
Hope this helps anyone!
I am creating a chrome extension that requires a back end server for processing. I would like it to send data to the server, the server will process this data and send some other data back to the user. Preferably this back end server should be in python as there are libraries I would like to use. Any idea how to go about this?
I was thinking to use a django server with rest API but I’m not sure what would be best. If anyone could provide a tutorial link or briefly explain the code behind this idea it would be brilliant.
I have produced a solution. You are able to send requests to a django server like any other server using JSON files. Here is my implementation:
mainproject/urls.py
urlpatterns = [
path("",include("sentimentProcessing.urls"))
]
sentimentProcessing/urls.py
urlpatterns = [
path('process_sentiment/', process_sentiment),
]
sentimentProcessing/views.py
# Create your views here.
@csrf_exempt
@require_POST
def process_sentiment(request):
data = json.loads(request.body)
# Process sentiment
sentiment = sentiment_prediction(data) # For example, here you could process data in your own way, using your own function
# Serialize the processed data to JSON
response = {"sentiment":f"{sentiment}"}
# Return a JSON response with the processed data
return JsonResponse(response)
There are also some installed apps for cors in settings.py.
Hope this helps anyone!