I am working on a small task.
the task requires me to process the data from a html form. the html form consists of a phone dial pad. the data is the phone number the user enters. I need to process the data. The functionality for processing of phone number (data) has been completed.
But i have a problem. I am used to django framework. But for this task, the database is not needed and i do not need a admin page as well. In short, i do not want to use a framework for a single web page.
Is there any possible ways for me to create a simple html form, and when i submit the form the data is transferred to the python code for processing.
I have been looking over through the internet, and i am not finding any suitable ways of implementing it. can you please help me out or point me in the right direction
I don't have an exact idea about Python Flask but you'll have to do the something similar to the following
from flask import Flask
app = Flask(__name__)
#app.route("/")
def calculation():
#manipute request here as per your
#pass manipulated data to the processing functionality already present
if __name__ == "__main__":
app.run()
Suppose this code now runs at http://localhost/, in HTML code, you'll have to write a form processing JavaScript code that'll parse the form. You can use any method .serialize() or anything else depending on what you are comfortable with. Then make an Ajax call to the running Flask code and to it pass the request parameters.
You can then probably host this Flask code on Heroku or something similar. Here is method that's creating RESTful web API using Flask. You can read through it and understand the workings of it. http://blog.luisrei.com/articles/flaskrest.html
Related
I'm creating a pretty large Django application and the whole front end works by making Javascript API calls to the back end and rendering the data, my only concern is the API I'm writing will also be used by other users from the command line or their own apps.
For front end to back end I'm just doing
if request.user.is_authenticated:
# Do shit
# Return Said shit but with JSON
How do I program in a way that this also works in a token based way? Or should every user have a default token that's used to make this API call?
How does a app like gcloud do this, I could pass objects to the django template, but I'm giving select options that make heavier API calls in the frontend. If there's a way I can move forward with this approach help will be much appreciated, and advice will be even more appreciated.
What is the best way to implement isomorphic react app using webpack with python as backend to serve
Following features are required to be implemented
1. React router
2. Redux store
By Isomorphic, I understand you mean both rendered on the server & on the client - so the first access of the site will return static HTML and a javascript bundle which then replaces the static HTML with the react-rendered version.
Usually this would mean running javascript on the server too with Node.js. This means you can render your JSX / React templates / components on the server and send them as static HTML to the user. If you're working with Python on the server, you cannot use your JSX / Javascript templating & logic on the server, so you'll need to duplicate it in Python.
So since you have to do it all twice - you first need to work out which one you want to do first. Either in Python first, or Javascript.
You can either start by building the whole application server-side-rendered, and then have javascript take over, or build a fully javascript application, and render the first views with Python.
Personally, for content-driven sites, I prefer building a server-side rendered application with Django (the most used Python Web Framework), and then adding Javascript on top once it's all fully working as a javascriptless site. This has the advantage of working even when javascript is disabled, and ensures you have good URLs, etc.
Of course, if it's a really complex application in terms of user interaction, you'll need to do it the other way.
I recomend looking into Django first, Here is a great tutorial from django-girls. However, if you want to go the mainly-JS route, here's some ideas:
Javascript First
Probably the best way is to first design your data structure(s), and make a REST api for your data. (e.g. /api/1.0/cars/rustbucket_94 which sends JSON data.)
Next, figure out your user-facing URL schema, and work out which REST endpoints need to be pulled in to get those pages. (How the URLs should look to the end users. e.g. /cars/rustbucket_94.html)
Design your React app as normal, using those REST / JSON endpoints, and the react router to show it correctly.
Once you have your whole react app working, you can now build a server-side rendered version of the whole thing, which you'll need to re-implement from scratch, and can either access the data through the JSON endpoints (slow) or by making the queries itself inside the pages.
The Python side
Which Python Framework to use on the backend?
I'd recommend Django to start with. It's very capable and can do ANYTHING you want it too. You probably need the Django REST framework too. There are other options available, but this will be the quickest way to get something secure and sane running.
Getting Django to work nicely with your webpack / react workflow is not too complex. There are projects like Django-Webpack-Loader and various tutorials about it online showing how to use it.
Good luck.
In this template i'm retrieving the price of Bitcoin from an API. At the actual moment, the price will be updated only when the page is refreshed, while i would like it to be updated without refreshing the whole page, dynamically.
This is my view:
def home(request):
symbol = "BTCUSDT"
tst = client.get_ticker(symbol=symbol)
test = tst['lastPrice']
context={"test":test}
return render(request,
"main/home.html", context
)
And the template's line looks something like this:
<h3> var: {{test}} </h3>
There are two problems here:
1) From the little i know, Django itself is not asynchronous, so i need to find a way to update that part of the template in real time, without having to refresh the whole page.
2) At the actual moment, the API is requested when the page is opened/refreshed, but to stream the price, it should be always running. I tried this (awful) solution: add a while true in the view, but of course it broke my code, executing only the part the while statement.
Any advice is appreciated :)
You should separate your frontend and backend in order to dynamically update the DOM contents without needing to render the whole DOM each time.
The responsibility of frontend would be to request and fetch the latest values when a user does an action that warrant a refresh or fetching of updated data. The request need to made asynchronously via e.g. AJAX. The newer JS frameworks e.g. React, Vue use virtual DOM that use an intermediate virtual DOM that they use to push the updates and finally update the real DOM in a single go; which makes them very performant.
The (Django) backend should expose an API that would take (AJAX) requests from backend for specific resources, and serve them.
A nice framework for Django is DRF (Django REST Framework), which exposes REST endpoints that you can call from frontend via AJAX and get reponse back so that the frontend can do necessary updates.
This is a failrly high level view, and is written to give you an idea that you can implement by digging deeper.
I have this project that I’ve been working on for a while:
In the views.py file, I scrape a lot of information from IMDB, with each call taking around 0.3 of a second. Meanwhile, my page will stand idle. I want to have it load the page and then finish up the call.
For instance, I want it to load the recommended movies after already showing the actors and actresses that played in both. Or in an index, I want to allow the user to type and then show the options to click on.
I’ve tried Celery with Redis, but Django can’t display asynchronous tasks.
How could I do this?
As you said, Django can't do that. My advice would be to divide and conquer:
First, write a view that just displays some HTML to load a JS script that can load your data asynchronously, using the render shortcut. A front-end framework like Vue can help you to hide the parts of your layout while your data loads.
Next, write views that just return the data using the JsonResponse object in Django, for example: one view to load the recommendations, one view to load the actor list.
Use XH requests to call your views and retrieve the information, using the Promise methods to make everything appear in sync.
Bonus: If you already have Celery in place, you can define a task that grabs all the data you need on the server, and create a view that polls the status of your task, and then call it using XHR every few milliseconds until your data (or part of it, that really depends on how you define your task) is ready.
I am building a web app with google app engine with python as well as HTML and CSS and I have a bunch of pages (around 15) that I want to make that will all somewhat different though will have a similar purpose. Is there any way to only have a few handlers that can create a bunch of pages or do I need a different class in python for every page?
I know this question is somewhat vague without context for the pages but really any information about how to create multiple pages without coding entirely new handlers and HTML for them would be a huge help.
No you don't need a different class for each page.
You can redirect every request to a singe script with app.yaml
handlers:
- url: /.*
script: dispatcher.app
Then, from dispatcher.py you can redirect every request to a single RequestHandler and program all your logic there, like serving a different jinja2 template for different URLs dinamically.
The URL Mappings documentation explains how to redirect multiples URLs to one RequestHandler. For example:
class BrowseHandler(webapp.RequestHandler):
def get(self, category, product_id):
# Display product with given ID in the given category.
# Map URLs like /browse/(category)/(product_id) to BrowseHandler.
application = webapp.WSGIApplication([(r'/browse/(.*)/(.*)', BrowseHandler)
],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()
It will really depend on what framework you are using (if at all), but what you are looking for is templating systems. Here is a good list of frameworks and templating systems.
It really depends on what you're trying to do, but if you're using webapp or webapp2, you can use one handler. Check the Request parameter's url field to find out which page the user is requesting.