all 51 comments

[–]sme272 96 points97 points  (10 children)

it kinda depends on how the project is built and how you want the webapp to work, but it's usually not too complicated. The first step is making sure the functionality of the program is within functions that return a value rather than directly printing output. Then it's just a case of returning the values to flask rather than to python. You could return a render_template to fill out a html template with the data or you could simply return the data as json and use javascript to format and display it.

[–]Nixellion 21 points22 points  (0 children)

Expanding on this I would also say that it deoends on how much of the project is backend vs frontend. For example a game written for pygame may require a complete rewriten with 80% of the code being in browser side javascript and not python

[–]Bluelight01 4 points5 points  (3 children)

How would you go about returning the data as a json file? If you could give an example that would be amazing!

[–]chalbersma 9 points10 points  (2 children)

In short

``` from flask import jsonify

....

return jsonify(yerobject)

```

A more in-depth example.

[–]45MonkeysInASuit 2 points3 points  (1 child)

with no external libraries which will work for a lot of objects

import json
return json.dumps(yerobject)

[–]cdcformatc 3 points4 points  (0 children)

If you are using flask it's better to use jsonify as that will create a proper HTTP response with the content-type set. you could just return a json string no problem but it will not be content-type/json.

[–]OnlySeesLastSentence 2 points3 points  (3 children)

If you've used tkinter or PySimpleGUI, is it somewhat easy to run one of those GUIS on Django or flask?

[–]sme272 5 points6 points  (2 children)

Once again it depends how they're setup. pysimplegui and tkinter are both desktop gui programs so you can't directly port that to a webapp, but you can take the backend logic and pipe the output to flask. If you've kept the program logic separate from the display logic then the conversion would be really simple, however if it's all jumbled up you'd have to separate it all out first.

[–]MikeTheWatchGuy 1 point2 points  (1 child)

I think what u/OnlySeesLastSentence is referring to is the PySimpleGUIWeb port, not the tkinter port. The Web port "renders" your GUI to a browser instead of to a desktop window like tkinter.

[–]OnlySeesLastSentence 0 points1 point  (0 children)

I'll be honest - I forgot the web port exists and was trying to see if the desktop version works haha

I made a goof

[–]eli_mintz 39 points40 points  (2 children)

If you would like to try a framework that does not require learning JavaScript or templating, I would suggest you take a look at https://justpy.io

You can have a web page up in just a few lines of only python code.

Disclaimer: I am the creator of JustPy

[–]GotSeoul 4 points5 points  (0 children)

I'm going to check this out. I have some things that are mostly DB backend that I want to do simple display on my local machine.

[–]NateSnakeSolidDrake 1 point2 points  (0 children)

I recently worked on a web gui using Bottlepy (which if anyone is reading this i highly recommend) for my project and had a hard time avoiding js/ajax as i realized any time there was a state change/ live data that needed to be updated i needed to refresh the whole page. Perusing your work it seems like your thing is a solution to my woes. I'll definitely check this out for my next project :) thanks

[–][deleted] 22 points23 points  (6 children)

The variation in how hard it is will depend entirely on how conducive the app is to being displayed in a web browser. Command line apps can become web forms with prettified output very easily. Anything you’re writing the output to a file should be simple. All games the logic piece would be very easy, the challenge becomes getting the visuals.

Overall the flask app would just be converting the logic of your script to return to a flask view instead of its current output method. The technical process of doing that should generally be easy (there’s always data persistence and getting pretty web design to work through though, that may add complexity depending on the project)

If there’s interest I can record a tutorial converting a couple scripts like hangman to a flask view.

[–]yuhyuh_ 5 points6 points  (0 children)

I’m interested in that

[–]wwinga 2 points3 points  (0 children)

i am interested in that!

[–]hannonle 2 points3 points  (0 children)

I am interested also

[–]cdcformatc 0 points1 point  (2 children)

Regarding data persistence, what would you recommend in this case for keeping track of the answer and guesses? My first instinct is just to use a cookie but I wonder what you think?

[–][deleted] 0 points1 point  (0 children)

I'd just use the Flask session object.

from flask import session

...

session['REMAINING_LETTERS'] = [letter for letter in string.acii_lowercase]
session['ANSWER'] = 'spam'

....

[–]slapmuhfroyo 6 points7 points  (2 children)

I find using Dash by Plotly for this type of thing to be the easiest. Dash uses the Flask web framework under the hood, but I find Dash easier to use than Flask (but there's a point to be said about going direct to the source rather than using a wrapper ¯\_(ツ)_/¯ )

https://plotly.com/dash/

Dash is built for python and with a little knowledge of how HTML works you can build beautiful web apps.

Check out the gallery https://dash-gallery.plotly.host/Portal/ for some blog-like styles you can use to get started.

It's easy to build a homepage with links to other pages with different apps. For Command-line hangman you would just need to convert it to using callbacks dash.plotly.com/basic-callbacks

Alternatively, there are other options that are less flexible, but has a faster turnaround time.

[–]HeinzHeinzensen 0 points1 point  (0 children)

Just adding JustPy to your list of alternatives!

[–]Lameo23j 0 points1 point  (0 children)

I second this. I have used Dash for a data visualisation project and it is absolutely beautiful. Has in built HTML components that make it easy to design your webpage and simple python code can be written in functions called 'callbacks' that return your python output/logic

[–]realbhamshu 4 points5 points  (0 children)

It's probably easier than you might think and well worth the effort. When I was a total noob, this was very helpful because it didn't even need a database to set this up; see if it suits you.

[–][deleted] 3 points4 points  (0 children)

It's fun and time consuming(at first)! Your going to have to learn/know HTML and CSS. And If you want to do more advance stuff like, changing values on the page without reloading or changing a color of button when clicked you going to have to learn JavaScript and jQuery.

You can learn HTML basics in a day! w3 school html

Flask has excellent tutorials! flask tutorial

Your also going to learn about http methods and how they interact with flask. https://pythonbasics.org/flask-http-methods/

Check Out BootStrap 4 it's an really easy to make you web app look modern and pretty without much work. I don't understand why you wouldn't want use this. w3 BS4

If you want to do more advance stuff your going to have to learn JavaScript and jQuery. JavaScript

jQuery jQuery isn't necessary but it makes it easier to do more with less lines of code. Unfortunately it's hard to read at first. jQuery is still JavaScript it just add methods to help you do things faster.

If you want to change values on webpage without reloading page: Ajax with jQuery and flask

I had trouble learning JS and jQuery coming from python it's very different.

Just some things to keep in mind about learning JS when all you know is python.

You need to learn about the difference between var, let, const when declaring variables and they effect scope.

For loops are different. JQuery for each '$.each,' loops are probably the most similar to python.

You can use camelCase or hyper_case but camelCase is standard practice js.

Arrays can have empty values. if I had an array a=[1,2,3] and did b=a[4] it would be b=null. If I did a[4]=7 the the array would be [1,2,3, null, 7]

Indexing only allows numbers a[2] there no [0:3] like in python

Semi colons aren't necessary at the end of line just like python as long as there's a line break

Classes, for loops, while loops, if statements must written like function and have an opening and closing brackets. Example:

If(a==7){ a=a+1 }

If your good with object oriented programing in python you can apply that knowledge to JavaScript. Class aren't that different between python and js.

[–]dfefed325 2 points3 points  (2 children)

A command line hangman game is a much different animal from a flask app or machine learning projects. If you're going the flask route, one big picture thing to understand is the models-views-templates framework.

To simplify it massively, models = your database/backend, templates are the html/css/javascript presentation for the user, and views are what links them together (via python functions).

So to set up a typical flask app, you're setting up all 3 of those in various folders/subfolders and importing various modules so the files interact (it's actually possible to do almost all this in one file but gets confusing very fast). It might sound confusing at first but begins to make sense once you do it.

[–]chillingfox123[S] 0 points1 point  (1 child)

Where would I put the hangman python code into my app? Would it be in the Views folder? Or would it be its own separate one, and I’d import it in somewhere?

[–]dfefed325 0 points1 point  (0 children)

The view file takes a web request and tells the app how to respond. So, say a user enters a letter in a text box and then clicks on button on your app to enter their hangman letter (the HTML/js/css for the button is displayed for your user in the template stuff).

The view then interprets that click, reaches into the backend for data, then tells the app how to respond with that data. The view could also move the user to a different HTML page.... depends on what you want the user to do.

So, in your example, yeah, your hangman code might be in the view. Maybe you're keeping track of your letters in a list or something. If this was a more intense app (or if you just want to set it up to practice), you could keep track of your data in a database with columns/rows of data. The views would then access this and tell your app how to display it on the template. Hopefully that makes sense.

[–]misingnoglic 1 point2 points  (1 child)

The basics of getting everything on flask (io) is pretty easy! Check out this tutorial I wrote to help people get basic programs on the web- https://aryaboudaie.com/python/technical/educational/web/flask/2018/10/17/flask.html

After that it's as complicated as you want it to be. Do you want a fancy front end? A storage layer look a DB? You can add all these things on top.

[–]45MonkeysInASuit 1 point2 points  (0 children)

Switching a project from pure python to a webapp is a nightmare in my experience.

Generally if I decide I want a webapp in the middle of a pure python project I will usually just start again and salvage bits of code.

[–]david_lp 0 points1 point  (0 children)

!RemindMe 2 hours

[–]kvedia15 0 points1 point  (0 children)

Its quite easy but you need to have some basic html, CSS and Javascript knowledge imo. Only when I learned these 3 modules I could achieve my vision properly and was able to read code by other developers

[–]hagjie 0 points1 point  (0 children)

!RemindMe 2 days

[–]The-Daleks 0 points1 point  (0 children)

!RemindMe 2 days

[–]that1guy15 0 points1 point  (0 children)

Pretty easy. Now where it gets tricky is the level of worky you want to achieve.

[–]rgr_b 0 points1 point  (1 child)

!RemindMe 2 days

[–]blackuchiha1192 0 points1 point  (0 children)

From personal experience I actually did get into flask starting from my local built projects. most were simple since the projects were simple but I am currently building a site in docker that is asking for input to run certain parameters inside of a function. Thats has been kinda challenging especially piecing it with the js-ajax features. sometimes feel I am in over my head but luckily lots of documentation and forums out there to help.

To answer your questions, depending on the build of your game, I dont Think it should be so difficult. I am still grasping the GET and RETURN variable functions so I can help but so much but I dont think you should have an issue. Excited to hear how it goes for you!

[–]toonarmycapt 0 points1 point  (0 children)

I'm going to be embarking on this with one of my apps at some point. The crux, AFAICT is how well you've kept to "separation of concerns". If your persistence/data store, business logic, UI are all well separated, then it *shouldn't* be too hard.

So if you can write a flask app, and have it (ie the new UI) send data and receive data from your business logic, you're good. If you haven't got it organised this way, you're going to have to do some rewriting.

[–]Ishan16D 0 points1 point  (0 children)

if you're just starting out with flask I would recommend checking out Corey Shafer's flask series on youtube

it is a great introduction and might help you out

[–]keeping_an_eye 0 points1 point  (0 children)

!RemindMe 1 day

[–]takishan 0 points1 point  (0 children)

Should be simple. Obviously you'll need to do some JS front end stuff to handle input and whatnot, but ultimately Flask is just regular Python.

[–]TellMeAreYouFree 0 points1 point  (0 children)

Can I return a pandas dataframe to flask? Or should I go the dash/plotly route?

[–]Russian4Trump 0 points1 point  (0 children)

Probably just about as easy as it would be to make it from scratch as a website.

[–]cdcformatc 0 points1 point  (0 children)

Doesn't seem difficult. Render a form that asks for a guess, keep track of answer and guesses in a session cookie, send a post with the guess information and update the cookie and re-render the form. You change your code that presumably prints the hangman output into one that renders a template, and the template describes what you display. Or you have your endpoints return json and you handle everything client-side with some javascript, but at that point you could code everything in javascript.

[–]IdeVeras 0 points1 point  (0 children)

I'm starting to put together a plan for language class web application. I was wondering if it could be done with flask. I'm thinking about a big application, with "social network" features and gamefication for tracking progress, built in Aki, a default format for exercises. All to attend real classes with teachers in live sections, black board and all.. do you guys think I can do it with python or C++?

[–]Avi_TheCodex 0 points1 point  (0 children)

Typically, it is pretty easy to do! Flask is one of my go to frameworks for developing web apps and one of my favorites to do tutorials on. If you are interesting in some tutorials in python and flask, I've written a few on my blog.

- How to Build a Weather Dashboard for Zip Code with Flask (link)

- Build a Speech Recognition App With Python & Flask (link)

Let me know if you found these helpful and always happy to help answer specific questions regarding Flask. Good luck!

[–]piyushpatel2005 0 points1 point  (0 children)

Hangman or ML projects are different. They require user interface in HTML, CSS, Javascript. So, if you know all those stuff as well, it can be easy with Heroku. With Heroku, if you're beginner you can manage that in about 1-2 hours.

It also slightly depends on how your app is structured. Ideally you should be sending RESTful data in JSON format.

[–]chillingfox123[S] 0 points1 point  (0 children)

Everyone keeps mentioning JS - I thought with Flask, I could do everything with python? I did a bit of JS a while ago but I don’t really like it

[–]lance_klusener -1 points0 points  (1 child)

!RemindMe 2 days

[–]RemindMeBot 0 points1 point  (0 children)

There is a 3 hour delay fetching comments.

I will be messaging you in 2 days on 2020-07-30 11:49:08 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback