how to display profile in a already opened tab in flask? - python

I am working on a web application in the flask. My main page displays the list of author names. When I click on the name, a new tab opens to display the author's profile information. If I click on another name from the main page, another tab gets opened.
I want a solution to open only one tab if I click on the author's name one by one e.g previously opened tab should get closed before opening new tab.
Is it possible?
thanks in advance

The web browser itself is responsible for opening / disposing of tabs when you click on links, and the idea of a "tab" doesn't necessarily even need to exist, some browsers don't have such a thing. For example, the mobile browser Firefox Focus can only have one page open at a time.
previously opened tab should get closed before opening new tab. Is it possible?
To put it simply, no. There is such a thing as javascript's Window.close(), but this can only apply to the page that the code was executed in, you have no control over other tabs that a user may or may not have open on their local machine. If you consider the example of Google Chrome, the basis of the "tab" construct is that tabs are isolated objects, in fact they each get their own OS process and don't directly communicate.
So what can you do to address your problem? Here are a few ideas
When an author is clicked in the list, use javascript to insert a new element containing their profile information without loading a new page. You could add the javascript tag to your queston to try and get some help in that area
When an author is clicked in the list, use flask's templating to reload the same page but with the selected author's profile template embedded in the page. https://jinja.palletsprojects.com/en/2.10.x/templates/#include
In either case, this question seems to be escaping the domain of Python, consider re-tagging or opening a new question with more focus

Related

When scraping using selenium, url opens in new window. it should be in the new tab

When I scrape the URL, it directs to the log in page. After logging in, it opens to new window and what is worst, it returns system error.
Irrespective of the url getting opened within a new window or within a new tab, all you need is to switch through the window_handles to access them one after another.

Get the tab the user is viewing in selenium

Is there any way to determine which tab the user is viewing, like say the active tab (in selenium) is the last one and same for the user, then the user goes back to the first one, how do I know this with selenium?
From the information that has been gathered there seem to be no way to do this. What you can do is see if the current window is the active one using:
driver.execute_script("return document.visibilityState") === "visible"
But you cannot loop through the tabs and check this property since as soon as you change tab the tab you change to will also become the visible one.
While switching tabs you need to change Selenium's focus from one tab to other.
You can find a relevant detailed discussions in:
Open web in new tab Selenium + Python
How to switch window handles using Selenium and PythonHow to switch into Window using Index in
Selenium
Selenium may not have the focus on the tab you are currently visualizing. It's the user's discretion on which tab you want Selenium to focus.
You can always extract the current window handle on which Selenium have the focus using the current_window_handle attribute as follows:
print(driver.current_window_handle)

How to open dynamic links in new tab with web py framework?

I tried web.seeother("link"), but this does not open it in a new tab. Now I can generate a link with a _blank tag, but then the user has to click on the link separately that is separate button for generating the link and another button to follow that link. I want to perform both with a single click. A server side method to do this would be best.
I am using the web.py framework.
As the document says web.seeother() is used for redirecting a user to another page. So a more clear way for asking your question is: "how to make web.seeother() open a link in a new tab"?
As I have observed the documents, There is no way to do that on server-side.
Not a web.py issue. Cannot be done from server-side by any python or non-python framework, must be done in the Client.
From the client, you can set target="_blank" in the HTML, or use javascript with something like window.open(url). Javascript will allow you to set size and position of second window.

Refresh a tab on a browser (Python)

My program opens a certain page on using
webbrowser.open(url)
How is it possible to reload the tab containing the url several times?
I could use sleep to set the time limit in which it has to wait before it has to reload.
But how do I refresh the tab after that? (Not open it in a new tab.)
I don't think it would be possible to implement a pure python solution for this which works with different browsers. A solution I would think of is using JavaScript. Vaguely the idea is to create a html file which has an iframe with the url you want and has javascript for reloading the iframe in regular interval. Then use webbrowser module to open that file.
This may sound ugly but this may be the only solution given the security concerns of a browser.
*If you are interested with this idea I can help you writing the code for this.
Hope this helps.
EDIT: below is my OLD answer, I'm not deleting it because it shows the ambiguity in the docs, and could possibly serve as a learning experience to someone.
If you read the docs, they make it sound like its possible. However, it is not possible to do with this module, further more, it seems like no matter what option you give to "new" it always opens in a new tab. Perhaps this behavior is specific to my system, or browser(IE9) but I believe it is more likely a bug in the program.
I investigated further, there is questions about this all over SO. you can't do it with webbrowser or anything built into python.
If you install selenium, you should be able to do what you want.
I am assuming you don't have access to the source code of this webpage, otherwise, you could just use html to do the refresh. If you don't want to install selenium and don't have source access, then you need to make a wrapper for the webpage, and use HTML/JS to refresh the wrapper.
the docs say:
webbrowser.open(url, new=0, autoraise=True)
Display url using the default browser. If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible. If autoraise is True, the window is raised if possible (note that under many window managers this will occur regardless of the setting of this variable).
so...
to refresh the page, it would just be:
for i in range(refresh_limit):
time.sleep(wait_time)
webbrowser.open(url)
^^^ this does not actually work^^^

Selenium unable to switch to TinyMCE iframe in Internet Explorer 9

I'm trying to swtich to an iframe in IE9 so I can send_keys() to the text area. When I do switch, I can tell that the webdriver thinks it switched (when I print page_source, it's right) but the cursor is still blinking on another textfield (not TinyMCE), at this point, if I send keys, the keys get appended to the other textfield and not to TinyMCE.
So I've been trying workarounds, If I select the the tinyMCE iframe and click(), the cursor is in the right place and I can send keys but the I can't return (switch back to the original frame/window) to submit the input.
Has anyone else run into this in IE9, are there workarounds?
This works, in Firefox and Chrome, just not IE9.
I had a problem like this once, and its quite complicated to work around it since TinyMCE generates some dynamic content. What I ended up doing to manipulate the contents of the TinyMCE editor was calling the API directly via page.execute_script and just doing it all on JavaScript.
A sample of my JS code is:
jQuery('textarea.tinymce').tinymce().setContent('test text in editor');
jQuery('textarea.tinymce').tinymce().selection.select(jQuery('textarea.tinymce').tinymce().dom.select('p')[0]);
jQuery('textarea.tinymce').tinymce().execCommand('Italic','true');
jQuery('textarea.tinymce').tinymce().execCommand('Underline','true');
jQuery('textarea.tinymce').tinymce().execCommand('Bold','true');
The first line adds text in TinyMCE's textarea, the second selects it (simulating a user cursor select), the third, fourth and fifth just manipulate the controls.
.execCommand() was particularly useful for activating the different extensions. After that I just validated that the form fields I was using were set with the expected HTML tags and called it a day.
I hope it helps!

Categories