Running python scripts from within java class Android Studio - python

I want to integrate python with my android app. What I want is:
I will write a python script and I will put it somewhere in my project folder. And I should be able to call that script from my activity class .java file.
In simple words, I want to build my complete app in Android Studio only but I want to use python for some parts of my code.
I know of only one thing that perfectly fits this criteria and that's "Chaquopy" but I don't want to use Chaquopy.
Can you please suggest something else?
Thank you

I would use flask and have an endpoint like so:
#app.rooute('/data', methods=['GET'])
def meth():
# python code here
return make_response(jsonify({'results': ret}), 200)
I've actually set up an endpoint for you to use here that takes a png file, uses pillow to resize it to 1200 pixels and returns the new png.
Your task is now to display the PNG however you would in Java.
EDIT: There are many, many approaches to read data from the HTTP endpoint in Java, one is given below, using okhttp:
Request request = new Request.Builder().url("https://hd1-martin.herokuapp.com/data").build();
Response rawResponse = new OkHttpClient().newCall(request).execute();
byte[] response = rawResponse.body().bytes();
This is the limit of my expertise, I'm now leaving the rest in your capable hands to get the new image to display in Android, with the following hint... I suspect you're going to be looking at writing the bytes -- in response in the snippet -- to a file and loading it into an Android ImageView.
Hope that helps.

Related

It it possible to write C# code in backend and send results back to dart?

Hi so I am making a website for an esport organisation. They want players to upload their Fortnite Replay file (.replay) and get stats from it.
I've found this C# library (also in python) that can read those files and extract stats:
https://github.com/Shiqan/FortniteReplayDecompressor
Since there's no dart package for doing that, I don't really know how to do that.
In other words, how to use C# as a backend for flutter? How to upload a file, do something with it in C# or python and then return extracted stats from it?
Thank you.
Dart's Process class allows you to execute an external command and optionally retrieve those results:
import 'dart:io';
main() {
// List all files in the current directory in UNIX-like systems.
Process.run('ls', ['-l']).then((ProcessResult results) {
print(results.stdout);
});
}
So in your case, rather than executing ls like the code above, you could write a simple program in C# or Python using the library you mentioned, and have it print the results to the console in some structured format.
Then you use the Process class to run that command, read the results, then parse and display them.
See the documentation of the Process class for more details.

integrating a web server into a python script

I have written a program to generate sequences that pass certain filters (the exact sequences etc don't matter). Each sequence is generated by making a random string of 40 characters made up of C, G, T or A. When each string is generated, it is put through a set of filters, and if it passes the filters it is saved to a list.
I am trying to make one of those filters include an online tool, BPROM, which doesn't appear to have a python library implementation. This means I will need to get my python script to send the sequence string described above to the online tool, and save the output as a python variable.
My question is, if I have a url to the tool (http://www.softberry.com/berry.phtml?topic=bprom&group=programs&subgroup=gfindb), how can I interface my script that generates the sequences, with the online tool - is there a way to send data to the web tool and save the tool's output as a variable? I've been looking into requests but i'm not sure it is the right way to approach this (as a massive python/coding noob).
Thanks for reading, I'm a bit brain dead so I hope this made sense :P
Of course, you can use requests or urllib
Here is demo code:
with urllib.request.urlopen('http://www.softberry.com/berry.phtml?topic=bprom&group=programs&subgroup=gfindb') as response:
html = response.read()

How to deliver an image array from python to Rails

I am new to the world of Web Developing, i am currently using Ruby on Rails to code my webpages.
Here is the problem i am facing right now.
I am trying to make a webapp (using ruby on rails) in which the user must submit an Image(i am using CarrierWave for this) and the program its suppose to do some image processing, recognized objects and count them, so for this task i made a python script which do this automatically(using OpenCV). My problem is that the python script must return a new image which has the objects of interest in rectangles, and then i must store it in the dabase.
Currently i am returning in console where my objects are in the image, and displaying the new image, but i am not able to return the image as an array so that ruby framework can process it and upload it.
A simple view at how i am doing this
In the example1 i am recognizing oranges in the trees.
If anyone can help me or have an idea at how to do it, maybe a different approach could work. I am new in this, i have researched everywhere with no goods results. I didn't want to pose any code because i dont know which part is useful or not. (sorry for my bad english, i am not use to it completely)
To save an image locally into python you need to make an Image object and then save it to a given path.
my_image= Image("simplecv")
my_image.save("my-image.png") # in the current working directory
or to a path :
my_image.save("path/to/img.png")
You can then re-load those files with another language and store them in a database.
More examples on :
http://tutorial.simplecv.org/en/latest/examples/basics.html

Python and downloading Google Sheets feeds

I'm trying to download a spreadsheet from Google Drive inside a program I'm writing (so the data can be easily updated across all users), but I've run into a few problems:
First, and perhaps foolishly, I'm only wanting to use the basic python distribution, so I'm not requiring people to download multiple modules to run it. The urllib.request module seems to work well enough for basic downloading, specifically the urlopen() function, when I've tested it on normal webpages (more on why I say "normal" below).
Second, most questions and answers on here deal with retrieving a .csv from the spreadsheet. While this might work even better than trying to parse the feeds (and I have actually gotten it to work), using only the basic address means only the first sheet is downloaded, and I need to add a non-obvious gid to get the others. I want to have the program independent of the spreadsheet, so I only have to add new data online and the clients are automatically updated; trying to find a gid programmatically gives me trouble because:
Third, I can't actually get the feeds (interface described here) to be downloaded correctly. That does seem to be the best way to get what I want—download the overview of the entire spreadsheet, and from there obtain the addresses to each sheet—but if I try to send that through urlopen(feed).read() it just returns b''. While I'm not exactly sure what the problem is, I'd guess that the webpage is empty very briefly when it's first loaded, and that's what urlopen() thinks it should be returning. I've included what little code I'm using below, and was hoping someone had a way of working around this. Thanks!
import urllib.request as url
key = '1Eamsi8_3T_a0OfL926OdtJwLoWFrGjl1S2GiUAn75lU'
gid = '1193707515'
# Single sheet in CSV format
# feed = 'https://docs.google.com/spreadsheets/d/' + key + '/export?format=csv&gid=' + gid
# Document feed
feed = 'https://spreadsheets.google.com/feeds/worksheets/' + key + '/private/full'
csv = url.urlopen(feed).read()
(I don't actually mind publishing the key/gid, because I am planning on releasing this if I ever finish it.)
Requres OAuth2 or a password.
If you log out of google and try again with your browser, it fails (It failed when I did logged out). It looks like it requires a google account.
I did have it working with and application password a while ago. But I now use OAuth2. Both are quite a bit of messing about compared to CSV.
This sounds like a perfect use case for a wrapper library i once wrote. Let me know if you find it useful.

GAE Python how to check file type on upload

So, i'm trying to create an google app engine (python) app that allows people to share files. I have file uploads working well, but my concern is about checking the file extension and making sure, primarily, that the files are read only, and secondly, that they are of the filetype that is specified. These will not be image files, as a know they are a lot of image resources already. Specifically, .stl mesh files, but i'd like to be able to do this more generally.
I know there are modules that can do this, python-magic seems to be able to do this for example, but i can't seem to find any that i'm able to import without LoadModuleRestricted. I'm considering writing my own parser, but that would be a lot of work for such a common (i'm assuming) issue.
Anyway, i'm totally stumped so this is my first stackoverflow question, so hope i'm doing well etiquette wise. Let me know, and thanks!
It sounds like you want to read the first few bytes of the uploaded file to verify that its signature matches the purported mime type. Assuming that you're uploading to blobstore (i.e., via a url obtained from blobstore.get_upload_url(), then once you're redirected to the upload handler whose path you gave to get_upload_url, you can open blob using a BlobReader, then read and verify the signature.
The Blobstore sample app lays out the framework. You'd glue in code in UploadHandler once you have blob_info (using blob_info.key() to open the blob).

Categories