Progress bar for Python Script in HTML - python

I've been working on a project running an NMAP scan in a web app using flask and displaying the results back to the UI. The NMAP scan seems to take a while to run. Can anyone tell me how I can add a progress bar to the HTML UI?
The script I am running Discovery1.py
import nmap
import pandas as pd
ns = nmap.PortScanner()
ns.scan('10.0.0.0/24')
print(ns._scan_result)
print(ns.csv(),file=open('results.csv','w'))
df = pd.read_csv("results.csv", delimiter=";")
df.to_csv("results.csv", index=None)
And the section of the HTML page Network.html cannot be displayed here for some odd reason. How can I achieve this and display in the HTML page.

Related

python GTK webview not reloading HTML file

using python Im trying to open my HTML file.
parallel Im loading data to my HTML but it is not reflecting in GTK webkit.
but GTK web view is not loading the runtime data but.
we have option to load data
map_webview.is_controlled_by_automation()
but is is not refreshing the page. any alternate option to refresh the webkit with new data.
below is my Code
self.map_window = self.builder.get_object("map_window")
self.map_webview = self.builder.get_object("webkit")
# self.map_window.fullscreen()
#self.map_webview = WebKit2.WebView()
self.data_file = "file://" + os.getcwd() + "/TrainMap/steptracker.html"
self.map_webview.load_uri(self.data_file)
# self.map_window.add(self.map_webview)
self.map_window.show_all()
self.map_webview.is_controlled_by_automation()
issue is resolved
used another approach(Pycario) to develop the web-view

how to open html file automatically using webbrowser package in python

I have a python script that use pandas and create dataframe from csv file and i want to display the dataframe information using pandas-profiling package and show the report in the browser once the user run the function .
But the system does not open the browser and display this error:
ValueError: startfile: filepath too long for Windows
code:
def displayDfInfo(self,df):
profile = pp.ProfileReport(df)
html_profile = profile.to_html()
webbrowser.open(html_profile,new=1)
where is the error and how to fix it?
I would simplify it to this:
import webbrowser
html = "myhtml.html"
webbrowser.open(html)

Python: How to open map created with folium in browser when running application

I have a very small application written in PyCharm using python3:
import folium
map = folium.Map(location=[58.1, 23.3], zoom_start=10)
map.save('map2.html')
This will create a map2.html which I can access in my browser by using pycharm and the url looks like: http://localhost:63342/iss-country/map2.html?_ijt=dcsefdg8om4ddfovlt5ooq6ro5
How can I automatically open this in my browser? So when I run the application it does not only generate the html page but also visits it immediatley. I found the webbrowser module which can be useful but how do I know the correct localhost url?
I don't see the issue with using the webbrowser module. Just make the file name and path a variable and call the webbrowser open method.
output_file = "map2.html"
map = folium.Map(location=[58.1, 23.3], zoom_start=10)
map.save(output_file)
webbrowser.open(output_file, new=2) # open in new tab

HTML button to execute python code

I am using flask to run my python code when html button is pressed. My python code takes an encoded image and returns the encoded text by decoding it.Now what I want is when html button is pressed, the page runs the python code and shows me the decoded text but when html button is pressed the webpage only shows the code in my py file but not runs it.
This is the python "new.py" code:
from flask import Flask
import os
import subprocess
app = Flask(__name__)
#app.route("/stop/")
def test():
os.system('Python CN_Project.py')
if __name__ == "__main__":
app.run(debug=True)
I am a beginner in networks ans python. Please help me.
Have you tried going through the Flask tutorial yet to get something simple working?
In the template, you can create a link to a python function:
log in
Yours would look like:
stop
And then in your view, new.py, import your python file as a module and call the function in there that decodes the image and returns the text.
from flask import Flask
from CN_Project import decode_text_from_image
...
#app.route("/stop/")
def test():
output_text = decode_text_from_image('hardcoded_image.jpg')
return render_template('my_page.html', text_to_render=output_text)
...
Then you want to render the output text in your html template:
<p>{{ text_to_render }}</p>
For now, work on just using an image that is hardcoded into your python function. Afterwards, you can deal with passing in the image, maybe through a filepicker.
References:
Flask tutorial
Flask templates
Flask view functions

How can I connect to a website using twill inside of google app engine's python sandbox?

this allows me to connect to a website with python on my computer:
from twill.commands import go, show, showforms, formclear, fv, submit
from bs4 import BeautifulSoup as bs
go('http://www.pge.com')
showforms()
this gets me to hello world on google app engine, with the twill and beautiful soup imports working:
import webapp2
import sys
sys.path.insert(0, 'libs')
from twill.commands import go, show, showforms, formclear, fv, submit
from bs4 import BeautifulSoup as bs
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World! I love dog food.')
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
now after this i try to connect to a website using twill and fail:
where can i call go() to connect to a website?
if I add it before class MainPage(webapp2.RequestHandler): it hangs and I don't get to hello world.
If I add it inside the MainPage class on the first line as either getit = go('http://www.pge.com'), or just go('http://www.pge.com'), it also hangs and I don't get to hello world.
If I add it inside def: get(self):, I get:
Internal Server Error
The server has either erred or is incapable of performing the
requested operation.
and a bunch of stuff about twill and mechanize.py, followed by
File "..../twill/utils.py", line 275, in run_tidy
process = subprocess.Popen(_tidy_cmd, stdin=subprocess.PIPE,
AttributeError: 'module' object has no attribute 'Popen'
am i somehow missing some other dependencies, like mechanize.py? or is there something else i need to be doing?
This helped partially solve my issue, but other problems cropped up later, particularly in filling forms, submitting them, and migrating further in the website.
import webapp2
import sys
sys.path.insert(0, 'libs')
from twill.commands import go, show, showforms, formclear, fv, submit, config
from twill.browser import *
from bs4 import BeautifulSoup as bs
class MainPage(webapp2.RequestHandler):
config('use_tidy', '0')
def get(self):
go('http://www.pge.com')
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World! I love dog food.')
self.response.write(show())
tidy is disabled in this way (tidy does not play well with the google sandbox), and I am able to show() the webpage via self.response.write(show()).
As a side note: be careful about using TextEdit to edit .py files when working with google app engine. I was getting all sorts of weird non-ascii character errors. I added # -- coding: utf-8 -- to the first line of the python file, which sort-of helped, and switched to using pycharm's ide which really helped.
twill is giving me all sorts of issues inside google app engine's sandbox that i am not getting on my system. i can get finally the html for one webpage, but can't submit forms in the way i was doing so simply on my system. showforms() isn't even showing the forms, maybe because i disabled tidy and the html is not being parsed properly?
i think one way to move forward here is to get tidy to work inside twill. obviously roadblocks are being hit "under the hood", and they are hard for me to see.
it seems like twill is a high level abstraction and maybe is not a good idea right now for using on google app engine. next i will try to switch to mechanize.py, or look for another sandbox, maybe amazon?

Categories