Browser as GUI for user input to a python script - python

Is there an easy way to use the Chrome as a GUI to allow a user to input data for further processing in Python. What I want to do is :
The user enters data into a table. The table is 5 rows by 4 columns. The user data is then processed in JScript and a weight average of each row is displayed in column 5 of the table. The user can then adjust the input data based on what shows up in column 5 or accept it.
Assuming the user accepts the data, it is then used in a python script (that is already written) for much more detailed calculations.
The python script is pretty detailed and no something that is easily re-written in JScript.I can prompt the user for each data input but I would prefer to have a simple table where all the data can be entered at once
Newbie here that knows HTML, a bit of JScript and a bit of Python and some R. But not ready to take on GUI programming in TK, QT, GTK, Kivy....etc.
What is the easiest way to do this ?

You can use flask for this.
You can make a python server, and process all the inputs in python.
Here is a great tutorial to start flask
Flast tutorial

Pick a web framework (Flask seems like the simplest to recommend for your case) and implement your page as a view (whose GET request implements a view that sends the JS and web form to the browser, and whose POST request implements a view that does the detailed calculations and sends the final response).

Related

I need to hook HTML input into Python and don't know how

I am working on a team project for a capstone course. We have decided to produce a self-hosted calendar app for college students. One team member has created HTML webpages for said self-hosted server. I need to hook button and text box input and output into a python program. We are using Starlette, but I cannot figure out how to get information from text boxes.
To give an example of what I need to do, the user will input information into a selection of text boxes. That info needs to be read and converted into variables, then sent into an SQL statement. I have the SQL done, I just don't know how to read the info from the HTML boxes and detect button input.
Before answering it is necessary to know if you are using a python framework, ideally it would be enough to change the action of the form and connect it with a python function.
Here I leave you a tutorial to clarify a little bit the panorama.
https://www.youtube.com/watch?v=dlhg8HMZTOk

Interfacing a QR code recognition to a django database

I'm coming to you with the following issue:
I have a bunch of physical boxes onto which I still stick QR codes generated using a python module named qrcode. In a nutshell, what I would like to do is everytime someone wants to take the object contained in a box, he scans the qr code with his phone, then takes it and put it back when he is done, not forgetting to scan the QR code again.
Pretty simple, isn't it?
I already have a django table containing all my objects.
Now my question is related to the design. I suspect the easiest way to achieve that is to have a POST request link in the QR code which will create a new entry in a table with the name of the object that has been picked or put back, the time (I would like to store this information).
If that's the correct way to do, how would you approach it? I'm not too sure I see how to make a POST request with a QR code. Would you have any idea?
Thanks.
PS: Another alternative I can think of would be to a link in the QR code to a form with a dummy button the user would click on. Once clicked the button would update the database. But I would fine a solution without any button more convenient...
The question boils down to a few choices: (a) what data do you want to encode into the QR code; (b) what app will you use to scan the QR code; and (c) how do you want the app to use / respond to the encoded data.
If you want your users to use off-the-shelf QR code readers (like free smartphone apps), then encoding a full URL to the appropriate API on your backend makes sense. Whether this should be a GET or POST depends on the QR code reader. I'd expect most to use GET, but you should verify that for your choice of app. That should be functionally fine, if you don't have any concerns about who should be able to scan the code.
If you want more control, e.g. you'd like to keep track of who scanned the code or other info not available to the server side just from a static URL request, you need a different approach. Something like, store the item ID (not URL) in the QR code; create your own simple QR code scanner app (many good examples exist) and add a little extra logic to that client, like requiring the user to log in with an ID + password, and build the URL dynamically from the item ID and the user ID. Many security variations possible (like JWT token) -- how you do that won't be dictated by the contents of the QR code. You could do a lot of other things in that QR code scanner / client, like add GPS location, ask the user to indicate why or where they're taking the item, etc.
So you can choose between a simple way with no controls, and a more complex way that would allow you to layer in whatever other controls and extra data you need.
If security is not a big concern: an API with a simple get method that takes as argument the object id and I will presume you have the code to make sure if the object is given as taken it will be switched to returned.
And why not post? POST needs headers that you can't include in qr unless you have a dedicated app, so GET and the ability to use example.com/api/leaseandret?id=12345 is a better alternative that allows for better usage with a QR.
A summary of the methods*
* A note here is that GET is not forbidden from being used to modify data and send data to a server GET is exclusively for getting data from a REST purist standpoint.

Execute python functions and read variables from html

First off, I am very new to programming and I have a relatively basic understanding of python, and average understanding of html.
Using what I know in python, I am trying to create a basic strategy game, a bit like the likes of Age of Empires, or Command and Conquer, based on collecting resources and using it to build things, except using a simple text or button-clicking type interface. I can do a text interface fine, but its a bit boring, and I would like to use some images. I have had a 1 hour lecture on tkinter, but I have tried and failed to make anything remotely 'usable' from it. What I can do, is make decent looking html pages which would serve my purpose very well.
What I am wondering is if there is a simple way of executing python functions and calling/displaying python variables through a html page? The python functions do all the logic and present variables which represent current resource levels, production, storage capability, levels of buildings, etc. At the most basic level all I need is a way of displaying these variables, and having buttons which execute a function to say, upgrade a building, which recalculates production and all that, and returns the new set of values.
As a really simple example:
<p> Wheat production: *python integer representing production*</p>
<button type="button" onclick="*execute python variable*">Upgrade Wheat</button>
There would also, I imagine, be a need to somehow update the variables which are changed on the html page. So the button executes a function to upgrade wheat production, python now has a new value for the wheat production variable, and this needs to be updated on the page, whether this is automatic, or by some other method. I guess the simple way would be if pressing the button could also reload the page, but that seems a little clumsy.
Does anyone know of a simple way of doing this? Or perhaps a python library which might help me here?
Yes, there is a way of doing this.
There are two approaches to this. One is to have all the work done on the server, the other approach is to use Javascript.
The first approach is this: write a python script that generates your HTML. If you use Django, you will get a lot of work done for you, but you will also get a lot of stuff you don't want. Django does have a built-in template language. Django is beyond the scope of this answer. You will get to do exactly what you describe above; an example of a template might be <p> Wheat production: {{wheat_production}}</p> - your python code will set up a dict mydict={"wheat_production":10} and you will pass the name of your template file and the dict to a function which will spit out your page. You will also have to learn about HTML forms, if you haven't done so yet.
The other approach is to use Ajax - Javascript that, when your page is displayed (and, perhaps, when buttons are clicked, or at regular intervals) will send/receive some data to allow you to update your page. I suggest looking into JQuery to do some of the lifting for you. This means that you can update bits of the page without having to reload the entire thing. You will still have to write some code on the server to talk to the database, and send the output, usually as JSON, back to the client.
When writing this sort of thing, make sure all of your security is on the server side, and don't trust anything the user tells you. For example, if you store the number of gold pieces in a field on your form, it's going to take someone about 10 seconds to give themselves as much gold as they want. Similarly, if a player can sell a diamond for 20 gold pieces, make sure they have the diamond before giving them the gold pieces - you don't want to end up with a player with 1,000,000 gold pieces and negative a thousand diamonds. Javascript is 100% insecure, anything that Javascript can do, the player can also do.
Take a look at http://pyjs.org/
What is pyjs?
pyjs is a Rich Internet Application (RIA) Development Platform for
both Web and Desktop. With pyjs you can write your JavaScript-powered
web applications entirely in Python.
pyjs contains a Python-to-JavaScript compiler, an AJAX framework and a
Widget Set API. pyjs started life as a Python port of Google Web
Toolkit, the Java-to-JavaScript compiler.
You can compile Python programs to javascript, and also use their Python libraries to generate HTML. Here's an example from their getting started guide:
from pyjamas import Window
from pyjamas.ui import RootPanel, Button
def greet(sender):
Window.alert("Hello, AJAX!")
class Hello:
def onModuleLoad(self):
b = Button("Click me", greet)
RootPanel().add(b)

Re-accessing python variables AFTER initial html get() call

I am making a data-plotting tool using google app engine and google docs/gdata and have run into a python/js/html communication issue.
I want a very simple layout for the webpage -- just a series of dropdown menu sections and then the final graphs. My current dilemma involves the dropdown menus.
There are total of 4 dropdown menus, and the fields of each one depends on the previous selection. I have found JS/HTML code that has all of the needed logic, but it assumes that you have pre-loaded all of the selection fields. Due to the scale of the project, it is impossible to load everything on start-up, and the data is dynamic/changing, so I can't just declare static files with the menu selection data contained.
Currently, I have methods to get all of the menu selections/data I need from google docs (using gdata). Ideally, I would like to just call each one in series (html selection1 -> python method1 to get next selection set -> html selection2 -> python method2 to get next selection set-> etc) but from what I can gather it seems this is not possible (return to python, ie 'post', without reloading the entire page).
So, is there a way to access python variables/methods in a google-app-engine JS code AFTER the initial template render or without 'posting'? Or, more open-endedly, if anyone has insight/suggestions/ideas I'd appreciate it.
Thanks
Two step process:
Create some server side code to return your data as JSON - the defacto method of transferring data on the web.
In your javascript code, call your server (from step 1) and then parse the result. Use the result to populate your dropdown.
The good news is that Google already provides json as an alternate format for their gdata APIs. See this link for a primer on how to use gdata and return JSON.
For the second part, almost all javascript client side libraries (and you really should be using one) provide an easy way to request data using ajax. As this is a common task, there are plenty of questions on StackOverflow on this - this one in particular has an example that shows you how to populate a dropdown using jquery and ajax.
Assuming you can use javascript and replicate whatever functionality your Python code is providing, the above will work for you. If you have some specific logic that you don't want to transfer to the client side, then your Python code will have to return json (in effect, you would be acting like the gdata api for your javascript code).
Since you didn't highlight which Python framework you are using - here is an example using flask, a simple framework for Python web development:
from flask import Flask, jsonify
app = Flask(__name__)
#app.route('/the-question')
def the_answer():
return jsonify(answer=42)
A more comprehensive example is available in the documentation under AJAX with jQuery.
If you are using webapp2, Google provides excellent documentation to do the same thing.

Dynamic image creation using Python over a web page

I'm new to learning Python and I've been trying to implement a text to image converter that runs on a web page.
I have succeeded in making the functional code that converts the text into image in Python, using the PIL module (i.e., user enters input text at run time and that gets converted into an image and is stored in the hard drive).
Now I want this code segment to work on a web page (something similar to feedback or comments form in websites).
a. That asks the user to enter a string in a text field and, on pressing a button, it goes ahead and converts it into an image and reloads the page.
b. once reloaded it displays the string-converted-image, and also provides the text box again for any new user to do the same.
I started with the Google Web Apps framework, and unfortunately I learnt that Google Web Apps can't support PIL module although it provides an image API, it can't support dynamic image generations based on user input.
So, can anyone guide me as to how I can go ahead and integrate a web page and the Python code that I have ready? So that it works?
Please guide me where I need to look and anything you consider is necessary to know in order to proceed
ahead.
Check out webpy, just about the simplest Python web framework. In the web.py module, import your PIL image converter, grab the text from the submitted form (via web.input()), convert the text and render a page with the new image on it. You should be able to get a simple page up in a dozen or so lines.
You can use any existing web scripting language to build the actual web page itself. Python (Django?), PHP, ASP.NET, Perl CGI will all work. Then if you're using a python based web framework simply call your python code directly after including the function in your code and serve the resulting generated image. Otherwise you can use a system/exec/passthru call to your code inside of a python script and send the text as input getting the source of the image as output.
To put this all into a webpage you would need the following:
A webpage with form and text box
A web scripting language page which serves an image after taking text as input through GET or POST. Example: Django Serve Image
Once the user submits a form it should load the results of the form data being passed to the script in either a frame or div below the form. This can be done in many different ways with Javascript or Frames.

Categories

Resources