Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a python script that takes inputs from commandline. I simply want to modify this script so I can run it on web. I want the commandline interface to be replaced by a simple boxes in a web page, and once the script is executed, I want the results to be shown on a webpage like it does in commandline.
Any help on where to start, which python packages to use and which steps to take would be much appreciated.
Until now, I read a little about webapp2 for Google App Engine and web.py. I do not want to use Django.
Thank you!
While you can use a microframework like Flask [1] for quickly getting started, you can get closer to the metal. Try learning about the HTTP protocol and implement your own server using the http module. Python 3's http.server contains a class SimpleHTTPServer [2] which can be very good for understanding how you're communicating between the client and your process.
[1] http://flask.pocoo.org/docs/0.12/quickstart/
[2] https://docs.python.org/2/library/simplehttpserver.html
Have you considered using Flask?
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
This is a pretty great tutorial on how to make a basic webapp.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I made a small script that solves an combinatorial optimization problem and I would like put it in a website so users can "play" with it, they could send a list of "points" to the server and this script would use a database to return the best combination of these "points".
The problem is I do not have much experience in web dev. I searched how to make an html button execute an script and I found this thread: https://stackoverflow.com/questions/48552343/how-can-i-execute-a-python-script-from-an-html-button#:~:text=To%20run%2C%20open%20command%20prompt,Hope%20this%20helpful.
But there says that an html button calling an python script is not safe. So what would be ideal What would be an ideal, safe alternative so that I could make sure that anyone who accesses my website can execute this script safely?
Well, there's no "easy" answer to your question. What you'd really need to do is to create a web-site in Python on your host computer – using a tool such as Django – and have one of the URLs supported by that website call your script.
Honestly, "what you're asking for here, really isn't the sort of question that StackOverflow is intended to answer." It's too big. Another one of the SE-family sites might be more appropriate, although I'm not quite sure which one ...
The solution that comes to mind would be setting up some Python-API (e.g. with Flask) which you could call with HTTP via JS, having different routes for different usages.
Here's a short overview of Flask showcasing how it could be used.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have made a python program. Out of curiosity, I want to implement it on a website so that my friends or anyone can run it without seeing the code directly.
Here's the Github link of my program code.
I have opened a free account on PythonAnywhere and added a new web app with the Flask framework.
It has a default file flask_app.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello from Flask!'
I have uploaded my .py file too. Having no experience with Python web apps, I've looked at the articles about Flask. Seems I need to translate my python code into some different structure. But I can't figure out how to connect and modify my .py file to the flask structure.
Can you kindly tell me how can I do so? Or is there any easy way else such that I just need to paste my python program into a file and it will be automatically implemented as a web app?
There's quite a lot of work involved in turning an interactive script like that into a website, but it is possible. This tutorial on the PythonAnywhere blog has a step-by-step guide that works up to a website that takes a series of inputs across multiple pages, and produces an output, so it might be a good starting point.
You can't just put this code into your flask api. There needs to be client rendering as well.
What you are looking for is a .io type web game.
Have a look at https://victorzhou.com/blog/build-an-io-game-part-1/
This article details the steps required for a browser based game.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Is there any way, through code, to prevent your app from idling? I'm thinking something like what is being done here. How to do this through code, but in python/django as that is what my app is done in, i.e. what is the equivalent in python to Node's setInterval. What are the disadvantages of using a pinging service over doing it through code(other than if the site goes down your app wont get pinged). Can you recommend a good pinging service? Thanks in advance.
You cannot do it from within the application. It needs to be woken up "externally". This can be just another Heroku application but that kind of defeats the purpose. There are some Heroku add ons that allow you to monitor your website thus also pinging it in regular intervals.
For example:
https://elements.heroku.com/addons/newrelic
With this your website would always stay awake.
The article you linked is imo more complicated than necessary. If you own a VPS you could host your website there. You could also write a curl command save it as a shell script and call it from cron (https://help.ubuntu.com/community/CronHowto).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
So basically, what I want to do is have a user input some data in an HTML form or something (on client end). Have that data be carried over to a server, where the data is put through some python code and the processed result is sent back to the client. I know, I could use javascript to do this on the user side itself, but I want to experiment a bit and make use of some libraries like tensorflow, matplotlib and so on.
Also, is there some way, you know like Web Assembly to run python code on the client side. Like maybe, send data from server or have it fed by the user, and on some virtual environment type setup and processed ??
Note: I know flask exists and I've tried it, but I can't see the same flexibility as you know regular python code.
Thanks in advance 😊
There won't come a definite answer to your question because your question is too broad. But maybe this will give you some starting points.
I see you have two questions:
How can I use python server side
How can I use python client side
Question 1:
First of all you might know that it makes sense to perform operations on a server and not on a client. For example interacting with a central database.
Flask is already lightweight compared to Django which also uses python. If you really want to do a lot on your own you could take a look at websocket or common gateway interface (cgi).
Question 2: This is really not recommended but if you want to play around with WebAssembly and Python a good starting point is PyPyJs: https://github.com/pypyjs/pypyjs!
You can use Brython in the browser, it's pretty spiffy. Full dom-manipulation from python; fully compatible with libraries written in pure python. Really neat stuff.
As for the server-side, if you want to keep it full-python, you'll need to use something like flask, bottle, cherrypy, aiohttp,...
If you find yourself struggling; maybe try starting out writing a simple socket-based microservice? You'll then be able to either farm requests out to it from any other server; or incorporate the code in your (python) server code.
Good luck!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Could you please tell me, How do I write a basic webpage without using framework like Django, Web2Py and others third party frameworks. Also, I don't like to use CGI for it. I need basic MVC structure with a hello-world web page only.
I suppose you mean a http server, because for a webpage only, you'd use html, not python.
I suggest you start with some reading on this page. It's the http server for python. As you want to keep things easy, you probably just want to overwrite the BaseHTTPRequestHandler or the SimpleHTTPRequestHandler classes, especially the do_GET and do_POST methods.
Note that this won't force you to use MVC, that's your own responsibility. You'll need an actual framework if you want to enforce MVC.