I'm a beginner web developer and I've spent so many hours trying to just do the following simple thing and have gotten nowhere... :( can someone help?
every 5-10 seconds, a file called latest_event.png will be updated on my computer with new contents. Using Python's Flask server, I just want to have my client application periodically poll my server and render the latest image in a web browser. I also want the user to be able to check and uncheck a box and their selection should be sent back to the server and their choice will affect how I render the latest_event.png image.
Alternatively, I've also explored Flask Socket IO libraries, but can't seem to get them working with image passing.
Would someone save the day and share with me the barebones server and client code to do this?
Thanks!
#app.route('/latest_event.png')
def latest_event():
return send_from_directory(os.path.join(app.root_path, 'static'), 'latest_event.png',
mimetype='image/png')
You can put your image into a folder named 'static' and push it out through flask this way.
Related
I have a React Native app where I want to send an image to my Flask backend to do some image processing (annotations) then return this new image back to React Native to display it.
I spent a whole day trying to figure this out but was unsuccessful. Does anyone have any ideas on how to achieve this?
I plan on using Firebase's storage system to store these images so I wouldn't mind using that either if that makes things easier.
What I've tried so far is sending the image uri to Flask and read the image file and was able to do the image processing however I couldn't figure out how to send the new image back to React Native...
How are you sending the image to Flask right now?
Typically you could implement an async function on RN that awaits a response.
In plain English:
A function that uploads an image to the back end and awaits for the image to be processed.
Expects in return a URL of the image on the back-end (or Firestore).
Problem: Habitica is a habit-tracking app, but its personal data logs are not as detailed as I want. I want to create a local log of when I mark off habits/todo's in the app. Habitica offers certain webhooks that trigger when habits/todo's are checked off, which seems perfect for what I want, but how do I turn these triggers into a local log? I would like to use Python for this.
Ideas: It seems to me that I would need to set up some kind of personal cloud server to receive this data, turn it into a log, and then store it for download. I have previously deployed a Flask app using Heroku, so if this could be done similarly, that would be ideal. However, I don't know much about this, so I would welcome any ideas or advice.
Creating the Habitica webhook as Flask application is a good approach.
Heroku supports Python/Flask very nicely however the file system is ephemeral, hence it gets wiped out at every application restart.
In order to persist data you can look at various options:
save the file to AWS S3
save the data into a DB (Heroku has a free plan for PostgreSQL)
I've been working on a project recently where basically I need to make a motor spin at certain times throughout the day for a few seconds, that can be customised using your phone.
So far I have followed many tutorials and done a lot of browsing and I've managed to have my Pi zero host its own network(using nginx, hostapd and dnsmasq), which you can connect to on your phone and go to 192.168.4.1 to access an index.html page in /var/www/html/
I also have a Python script which, when run, turns one of the GPIO pins on for a few seconds and then off again, and this GPIO pin is in turn connected to the motor.
The trouble I am having is setting up the rest of the web side, where you are able to connect to the network, go to a page, insert 2 or 3 different times, submit them, and then when it's that time the Python scripts will run.
Since I've set up the pi as a access point, I'm not sure how to reverse it and allow it to connect to wifi again without ruining the access point and current set up, so I'm not sure if there's an easy way to download any packages or modules I may need.
Anyway, any help anyone could give me would be incredibly useful - many thanks!!
Unless you are planning for a production web server, for simple application like display sensor status or control sensor via web page, there is a simpler solution for beginners and for python programmers. Since you are using python, so you don't have to use PHP, and you probably don't need to have Nginx at this stage. There are actually two ways in my experiences to do it.
1) Using http.server
The 'simple way' to serve web page using python based on python standard library http.server, utilising python build-in socket based http server. But it is less intuitive to set it up for GET/POST requests/responses. It is too long to describe it here, but I have a blog post on how to do it here.
2) Using Flask web development micro framework
Flask allows you to setup html template, handling route and run a web server easily within python environment. You need to install Flask package for python web development. The simplest flask python code that addressed your question of serving the data to a web page would be:
from flask import Flask, render_template_string
app = Flask(__name__)
data = 200 #assuming this is the data you want to show in your web page
#app.route('/')
def index():
return render_template_string('''
<h1>My Sensor Web Page</h1>
<p>My sensor reading is {}".format(data))</p>
'''
if __name__ == '__main__':
app.run(debug=True)
Launch your browser and point it to http://localhost:8000, you should see the data to be rendered as webpage per our simple example code.
What you will need is to either import your code into this flask example, or integrate it into the example, and pass the data you want to display to render_template_string function.
I would suggest using a web framework to host the "website". Flask is one that I have used for similar applications. Since this method allows you to directly call python functions in response to http requests it should be fairly easy to implement what you are trying to do.
As a bonus, you can use flask with nginx but I really don't think you need it for this specific application.
I am very new to Google App Engine coding and have no idea how to resolve this. What I am doing is a very simple thing. I have designed and made a website on my local PC. Now to host it, I am using Google App Engine. So what do I do, I simply downloaded the python app engine launcher and uploaded my entire website on Google Apps. I have set all the redirects on the control panel of Godaddy! which has registered my domain name.
Now whenever I visit the site for the first time, I get "Hello world!Status: 302 Moved Temporarily Content-Type" message and then upon clicking refresh, it loads the website. How can I get rid of this?
Much appreciate your help on this.
Thanks
Ok i decided to post the question here because i really don't know what to do or even if its possible. You might tell me it's a repost or so but i aready read similar posts about it and it didn't helped me out.
Here is the deal. I have an admin interface with django and want to download a file from an external site on my server with a progressbar showing the percentage of the download.
I can't do anything while it's downloading. I tried to run a command with call_command within a view but it's the same.
Is it because Django server is single threaded? So, is it even possible do achieve what i want to do ?
Thanks in advance,
It's possible but takes some jumps though the metaphorical hoops. My answer isn't Django specific, you'll need to translate it to your framework.
Start a thread that does the actual download. While it downloads, it must update some data structure in the user's session (total size of the download, etc).
In the browser, start a timer which does AJAX requests to a "download status URL"
Create a handler for this URL which takes the status from the session and turns that into JSON or a piece of HTML which you send to the browser.
In the AJAX handler's success method, take the JSON/HTML and put it into the current page. Unless the download is complete (this part is more simple with JSON), restart the timer.