问题
I have made a python program. Out of curiosity, I want to implement it on a website so that my friends can run it without seeing the code.
Here's the Github link of my program code: .py file, .ipynb file.
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 check my codes in Github and help me to implement it in the Flask framework?
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?
回答1:
There's quite a lot of work involved in turning an interactive script like that int 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.
回答2:
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.
来源:https://stackoverflow.com/questions/63095658/how-can-i-convert-my-python-program-into-a-web-app-on-pythonanywhere